C
C#2d 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 :)
No description
18 Replies
Nicholas
NicholasOP2d ago
This is how I assign the UIOverlay: (Done in inspector)
No description
Buddy
Buddy2d ago
However, for some reason it's a global variable.´
Please show code -- Always post code when you ask a question. $paste
MODiX
MODiX2d ago
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!
Nicholas
NicholasOP2d ago
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!
333fred
333fred2d ago
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 those
333fred
333fred2d ago
Namespace 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 namespace
Nicholas
NicholasOP2d ago
So 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)
333fred
333fred2d ago
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;
Nicholas
NicholasOP2d ago
Oh, sorry, it's different, it's refering to this line in the unity editor bob.UpdateTimer(gameTime);
Nicholas
NicholasOP2d ago
Which points to this in UIOverlay
No description
333fred
333fred2d ago
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?
Nicholas
NicholasOP2d ago
UIOverlay is a MonoBehaviour and also the script is attached to a canvas gameobject in the screen.
Nicholas
NicholasOP2d ago
No description
Nicholas
NicholasOP2d ago
No description
333fred
333fred2d ago
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 🙂
Nicholas
NicholasOP2d ago
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()
333fred
333fred2d ago
Don't forget to $close the thread if everything is resolved
MODiX
MODiX2d ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server