x


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 ?

more ▼

asked May 15 '10 at 10:54 AM

Fariz gravatar image

Fariz
36 4 4 5

(comments are locked)
10|3000 characters needed characters left

3 answers: sort newest

couple of things you are missing a " after Collect Cube. also 'True' should be a small t. And then what he said ^

more ▼

answered Nov 07 '11 at 11:38 AM

TheFrankman123 gravatar image

TheFrankman123
162 24 35 37

(comments are locked)
10|3000 characters needed characters left

couple of things you are missing a " after Collect Cube. also 'True' should be a small t. And then what he said ^

more ▼

answered Nov 07 '11 at 11:38 AM

TheFrankman123 gravatar image

TheFrankman123
162 24 35 37

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 15 '10 at 11:31 AM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x262
x239

asked: May 15 '10 at 10:54 AM

Seen: 1566 times

Last Updated: Nov 07 '11 at 11:38 AM