Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fariz · May 15, 2010 at 10:54 AM · booleanreference

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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ^

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ^

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

How to Get a PlayerPrefsX boolean? 1 Answer

Can't reference boolean in another script 3 Answers

How do I reference variables from other class files? 1 Answer

Checking a bool's state from another script? 1 Answer

issue with yield.WaitForSeconds(); 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges