• 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 /
  • Help Room /
avatar image
Question by AndreasX12 · Mar 17, 2013 at 12:08 PM · booleanvalueboolbooleans

How do I check if bool is true from another script?

Hi I have one script that says:

 public bool IsLightOn = true;

and then I have another script, that should be something like this:

 If (IsLightOn = true)
 {
  // Do Stuff
 }

But that wont work because the IsLightOn bool is in another script, how can I make the other script "get" the bool from the other script?

Thanks, Andreas.

Comment
TutorialNom
nilssanderson
gregroberts
osem598
Take_nokon
YUN-NI

People who like this

6 Show 1
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 fafase · Mar 17, 2013 at 12:15 PM 0
Share

You need GetComponent Check this one http://unitygems.com/script-interaction1/

Also, If is if, low i on the front and finally

if(IsLightOn = true) should be if(IsLightOn == true)

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by dorpeleg · Mar 17, 2013 at 02:14 PM

 if (GameObject.Find("name of the gameobject holding the script with the bool").GetComponent<name of the script holding the bool>().IsLightOn) //will check if true
 
 if (!GameObject.Find("name of the gameobject holding the script with the bool").GetComponent<name of the script holding the bool>().IsLightOn) //will check if false

If your both scripts are on the same gameobject. don't use Find.

 if (gameObject.GetComponent<name of the script holding the bool>().IsLightOn)//will check if true
 
 if (!gameObject.GetComponent<name of the script holding the bool>().IsLightOn)//will check if false
Comment
AndreasX12
_1
kiwiboys
AMU4u
sirop
Christian1DK
jeremyn
nilssanderson
KnightRiderGuy
FFVDGames
VaderCmp
Quast
Sajalsh25
Khudere
dreg_master
And 15 more...

People who like this

28 Show 15 · 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 AndreasX12 · Mar 17, 2013 at 02:49 PM 0
Share

Thank you so much for your answer, it worked perfectly :D

avatar image dorpeleg · Mar 17, 2013 at 02:58 PM 1
Share

You welcome :P

avatar image fafase · Mar 17, 2013 at 05:12 PM 1
Share

Note that if you do that in the Update that is quite bad. It will go fine until you have many objects on your scene. Then it will go slow, real slow.

avatar image AndreasX12 · Mar 17, 2013 at 05:39 PM 0
Share

I have it in the Update "void/function", how can I prevent that?

avatar image fafase · Mar 17, 2013 at 05:44 PM 3
Share

Create reference to the script (did you read the link I gave in the first place?)

 ScriptType script;
 void Start(){
     script = GameObject.Find("Name").GetComponent<ScriptType>();
 }

 void Update(){
     if(script.isLightOn)//Light is on
 }

Do not be think of creating a boolean variable in your script like this:

 bool isOn;
 void Start(){
     isOn = GameObject.Find("Name").GetComponent<ScriptType>().isLightOn;
 }

That won't do. Booleans are value type so without getting too deep, it would copy the original value but would not update it if the light would change.

Show more comments
avatar image

Answer by jackerlo · Sep 21, 2018 at 04:35 AM

?? cant see?

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If statement not working 1 Answer

seting a bool false/true,Bool which isnt supposed to be active is in the begining 0 Answers

Call a function When a bool changes value? 3 Answers

Boolean always false 0 Answers

Building a Face Generator, need to destroy previous instance after pressing key a second time 0 Answers


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