- Home /
Referencing a boolean from another script
Hi there,
I'm fairly new to scripting and I was hoping someone out there could answer my question.
I have 2 Scripts
The first script is a Char Controller that features this If statement. The code below basically is trying to say that if the character is stepping on the tile type called EndTile and if if the current stage is IntroRoom1 and if CollectItem which is a boolean from another script equals to true then load another stage
if(currentTileType == "EndTile"){
if(currentStage == "IntroRoom_1" && GameObject.Find("CollectCube).GetComponent("Collect").CollectItem == True){
levelToLoad = "Stage_1";
}
} The following script called Collect which is attached to a cube gameobject says that if our Main Character collides with the game object destroy the current game object and switch the CollectItem boolean to true
if(theOther.gameObject.name == "MainChar"){ Destroy(gameObject); CollectItem = true;
}
When I destroy the gameobject Collect Item turns to True but when I walk onto my EndTile object it says NullReferenceException: Object reference not set to an instance of an object
anyone know what I did wrong ?
Answer by Mike 3 · May 15, 2010 at 11:31 AM
It looks like you're destroying the object with the boolean on If it's destroyed, GameObject.Find will return null, and then using GetComponent with that will give you the exception
I would check if the return of the Find is null, if it is, then you know that the object was destroyed, and so you can go to the next level
Alternatively - if there is only ever one thing to pick up, you could use a static boolean and set it to true where you do now, set it to false on level load
That way, you just need to use YourClass.YourBool = whatever; instead of the find and getcomponent
Answer by TheFrankman123 · Nov 07, 2011 at 11:38 AM
couple of things you are missing a " after Collect Cube. also 'True' should be a small t. And then what he said ^
Answer by TheFrankman123 · Nov 07, 2011 at 11:38 AM
couple of things you are missing a " after Collect Cube. also 'True' should be a small t. And then what he said ^
Your answer
![](https://koobas.hobune.stream/wayback/20220612235632im_/https://answers.unity.com/themes/thub/images/avi.jpg)