visual studio – Unity – Camera glitches when displaying UI text (Item Name) for the first time


I have this very specific and wierd bug in my game that when I hover over a item in game and it should display its name it immediately change position of my camera to a random new angle. It always happens only the first time of running the game, so every time after this it works just fine. I dont have any function in my code for camera which should be doing this.

Could someone please help me?

Thanks a lot 🙂

My Text displaying code is here

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
 
public class SelectionManager : MonoBehaviour
{
 
    public static SelectionManager Instance {get; set;}
    public GameObject interaction_Info_UI;
    TextMeshProUGUI interaction_text;
    public bool onTarget;
    public GameObject selectedObject;
 
    private void Start()
    {
        interaction_text = interaction_Info_UI.GetComponent<TextMeshProUGUI>();
        onTarget = false;
    }

    
    
    private void Awake()
    {
        if(Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }


 
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            var selectionTransform = hit.transform;

            InteractableObject interactable = selectionTransform.GetComponent<InteractableObject>();
 
            if (interactable && interactable.playerInRange)
            {
                interaction_text.text = interactable.GetItemName();
                interaction_Info_UI.SetActive(true);
                onTarget = true;
                selectedObject = interactable.gameObject;
            }
            else 
            { 
                interaction_Info_UI.SetActive(false);
                onTarget = false;
            }
 
        }
        else
        {
            interaction_Info_UI.SetActive(false);
            onTarget = false;
        }
    }
}

I have already tried reseting the setting of main camera in unity but it didnt solve the issue and I have been looking inside my code for any line which could be moving the camera but it seems its not in code. So maybe its in some kind of setting? I have not found the answer on forum posts or google in general.

Leave a Reply

Your email address will not be published. Required fields are marked *