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 :)
18 Replies
This is how I assign the UIOverlay: (Done in inspector)
However, for some reason it's a global variable.´Please show code -- Always post code when you ask a question. $paste
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
https://paste.mod.gg/ceduulfgszbk/0 Sorry about how messy it is, it's a work in progress 😅 The main problem is I'm trying to assign "bob" in start (will be renamed dw) so I can use the assigned value rather than getting the componant every time I need it. It's used every frame in update which to my knowledge is very inefficient.
BlazeBin - ceduulfgszbk
A tool for sharing your source code with the world!
I think you're getting confused about the
global::
here
That's just the global namespace alias. global::UIOverlay
just means that UIOverlay
is defined in the root namespace
IE, doesn't have a namespace
That's it
It doesn't mean that the variable is a "global" variable; C# doesn't have thoseNamespace alias operator - the
::
is used to access a member of a...The C# namespace alias qualifier
::
is used to access a member of an aliased namespace. The ::
operator is often used with the global
alias, an alias for the global namespaceSo it should work using bob.UpdateHealth(); etc? as long as I assign it first in start()?
Because when I use it, it gives me this error:
NullReferenceException: Object reference not set to an instance of an object
GameManager.Update () (at Assets/Scripts/GameManager.cs:234)
So, I didn't look through the rest of your code
However, assuming the code you pasted is the same as the code you're running there, that
NullReferenceException
has nothing to do with bob
Line 234 is gameTime += Time.deltaTime;
Oh, sorry, it's different, it's refering to this line in the unity editor bob.UpdateTimer(gameTime);
Which points to this in UIOverlay
If it was null reffing in that method, you'd have gotten an exception saying such
I'm not particularly familiar with unity and its object lifecycles. Is
UIOverlay
a MonoBehavior
? Does it need to be initialized or something?
And are you sure that UIOverlay.GetComponent<UIOverlay>()
is returning a non-null object?UIOverlay is a MonoBehaviour and also the script is attached to a canvas gameobject in the screen.
Well, then my guess (again, not good at Unity) is that the backing object of
bob
isn't created
So it's not null
in terms of C#, but attempting to do anything on it will null ref
But at this point, you may want to take this question over to #game-dev so people with more unity experience can help 🙂Okay. I'll have a look. I really appreciate the help though :)
I figured out I was creating bob in Start() but in another script a method that uses bob was also being called in start, which meant it wasn't created yet. I just needed to assign bob in awake()
Don't forget to $close the thread if everything is resolved
If you have no further questions, please use /close to mark the forum thread as answered