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 Numberslayer · Jun 26, 2014 at 04:27 AM · c#gameobjectgetcomponentbooltransfer

Get bool value from one script to another (C#)

Hey guys. I have 2 GameObjects. I want to transfer a bool value from the script of object 1 to the script of object 2.

So say I have bool1 = false in script 1 to transfer over to bool2 in script 2. I want this to be dynamic so as bool1 becomes true bool2 becomes true as well.

Here is what I have tried. I have set a tag called Object1 to the first object and gave the first object a script called Obj1. This is the code in the 2nd object.

 bool2 = GameObject.Find("Object1").GetComponent<Obj1>().bool1;
 Debug.Log(bool2);

This compiles just fine but when I change the value of bool1 from false to true, bool2 just stays as false. No clue why it won't change.

Comment
Add comment · 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 supernat · Jun 26, 2014 at 04:34 AM 0
Share

Is this running in an Update method? And you aren't getting any errors in your console?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by crunchyoverseas · Jun 26, 2014 at 06:24 AM

If you have a tag set up you should be using findobjectwithtag. You'll be tagging the script that the bool is in. make sure at the beginning of the class (before start and update) where you want to change you have...

GameObject example;

Then in your start function you want...

example = GameObject.FindGameObjectWith Tag ("example_tag");

Then in Update or where you want to change it...

example.example_bool = false;

That should do it...

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 rohitanshu · Jun 26, 2014 at 06:24 AM

Your line of code will just execute once if not placed in a repeating function like Update() or FixedUpdate() etc.

Furthurmore, you should consider creating a function in Obj2-script that will update bool2 to bool1 as a passed parameter. Then, call that function using SendMessage(), like this:

 gameObject2.SendMessage("updateBool2", bool1);

also, I haven't tried this myself, but may be you could use Properties.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

gameObject.GetComponent("Script").enabled = true not working 5 Answers

Creating a single-line function for GameObject.Find and GetComponent (for multiple components) 3 Answers

Get components of multiple instantiated objects? 2 Answers

Setting a bool is not working with objects that have already spawned.. 1 Answer

GetComponent vs AddComponent 3 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