Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 alexanderameye · Jun 01, 2015 at 05:34 PM · scripting problemvariableupdatebooleanpublic

Access a variable from another script in update function

I have a script that constantly checks if a bool is true or false, and a second script that does something. Now I want the second script to only do something when the bool is returned true. So on runtime I want script2 to constantly check if the bool in script1 is true, so something like this.

 if(bool == true)
 {
 do;
 }

what is the most efficient, most elegant way to do this? Thank you very much!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by valyard · Jun 01, 2015 at 06:21 PM

I would do it this way.
In Script1 I would create an event like this:

 public event EventHandler<object,EventArgs> SomethingHappened;

and when something happens, i.e. your bool value is set I'd fire this event

 if (SomethingHappened != null) SomethingHappened(this, EventArgs.Empty);

In Script2 I would have a field referencing Script1 to subscribe to this event:

 public Script1 MyScript;
 
 void OnEnable()
 {
   if (MyScript) MyScript.SomethingHappened += onSomethingHappened;
 }
 
 void OnDisable()
 {
   if (MyScript) MyScript.SomethingHappened -= onSomethingHappened;
 }
 
 void onSomethingHappened(object sender, EventArgs e)
 {
   // do something
 }

And set MyScript in inspector. This way you don't need loops, nor Script1 knows what other scripts are interested in something happening inside.

Comment
Add comment · Show 3 · 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 fafase · Jun 01, 2015 at 06:41 PM 0
Share

SomethingHappened and onSomethingHappened should have the same signature.

avatar image valyard · Jun 01, 2015 at 06:53 PM 0
Share

Oops, forgot obvious this. Thanks.

avatar image alexanderameye · Jun 04, 2015 at 07:50 PM 0
Share

Hey, I managed to work it out before you answered :p and with fewer lines so I didn't take a closer look @ your code, but thank you so so much for your effort! you are amazing!

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

22 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 avatar image avatar image avatar image

Related Questions

public static bool not working at all. 0 Answers

Script not responding to public variable change 1 Answer

Cannot access AudioSource from within function 0 Answers

can't call script variable from another script 1 Answer

Update() doesnt call method or modify values 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