C#C
C#โ€ข13mo ago
Nicholas

Can't assign private variable (Unity)

Hi, sorry if I'm being stupid. I'm currently making a game in unity and have run into a problem. I've already tried searching online but I couldn't find anything.
I'm trying to assign a variable of the type UIOverlay which is another class.
I reference the gameobject that it's attached to using [SerializeField] public GameObject UIOverlay; and then in Start intend to assign the "bob" variable (temporarily randomly named to ensure no naming conflicts) using: bob = UIOverlay.GetComponent<UIOverlay>();

However, for some reason it's a global variable. I don't understand why or how this is. The reason I'm doing this is to prevent the script from Getting the componant every time I want to update the UI.
Any help with this would be great. Thanks :)
image.png
Was this page helpful?