- Home /
Accessing Static variable from another class gives null value!
Hi all,
I am stuck with this error for many hours ... Will appreciate your inputs on this.
I am trying to use a variable as a global by declaring it static in class "Init". Initially, I am returning the Gameobject using FindWithTag into a static Gameobject variable in class "Init" and using its SetActive property to set it to false. Later on, I am trying to access this variable from another class "csblock1" and trying to reset it to true using "Init.hidez[0].setActive(true)". At this point, I am getting a console message "NullReferenceException: Object Reference not set to an instance of an object"
Even though I have initialized the static variable in class "Init", how does it return null when I am trying to access from class "csblock1" ?
Thanks in advance, Anhallink text
Let's start with the obvious - have you definitely got a gameobject active in the scene hierarchy tagged with "zero1" at the start of the game?
@tanoshimi Yes I have. Actually, I am developing an x zero game for learning unity. ins$$anonymous$$d of x, i have cubes and ins$$anonymous$$d of zero i have spheres. The scene has these gameobjects and i had even succeeded in hiding these objects initially when the game starts. During this time, only black blocks are visible which are again cubes but not taking part in the game. Next, when I click on the block, depending upon the toggle flag, either a green cube or a pink sphere appears denoting either a x or zero. The init.cs is the initialization script and added as component to only one of the black blocks while the csblock1.cs is added to block 1, csblock2.cs to block 2 and so on upto csblock9.cs
Hi, I have one more observation. I have entered Debug statements in the Start() function. It appears the Start() function is called thrice. On first attempt, it stores the gameobject and on 2nd and 3rd attempts, the gameobject disappears. I have attached the updated Init.cs filelink text
Answer by rmassanet · Sep 19, 2016 at 12:40 PM
As you say in your comments, your Start
function might be called more than once. This is because your script is a MonoBehaviour
and, as such, it can be attached to many GameObject
s. However, they all share the static variable hidez
.
In addition, keep in mind that FindWithTag
will not find deactivated objects.
So, what is probably going here is this:
First instance of
Init
correctly finds the desiredGameObject
s and deativates them.Second instance of Init does not find any object, so it overwrites the static variable with null objects.
When csblock1 tries to access the static array, it finds only null values.
So, the solution: Make sure that there is only one instance of Init in the scene. Or even better, don't make this script a MonoBehaviour at all, unless you really need it. Make it a singleton.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Renderer on object disabled after level reload 1 Answer
Game freezes after restarting. 2 Answers