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 Rhithik · Jul 06, 2012 at 07:43 PM · c#variableboolean

Change a Variable with another script not working (C#)

I am trying to change a boolean variable that is in ScriptA with ScriptB. Here is a small example:

 public class ScriptA : MonoBehaviour {
   public bool followBall;

   void Update() {
      if(followBall)
          Make camera follow ball;
   }
 }
 
 public class ScriptB : MonoBehaviour {
 
 public ScriptA a;

   void Update() {
      if(space is pressed) {
            create object to follow;
            a.followBall = true;
       }
   }
 }

When I try to change the boolean variable in scriptA with scriptB it stays false. I made sure that I dragged the ScriptA component onto ScriptB. I don't know what I am doing 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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Jul 06, 2012 at 07:44 PM

You are doing that correctly - are you sure you are checking the right ScriptA when you see that it is still false? Are you sure the code here is executing?

Comment
Add comment · Show 5 · 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 Rhithik · Jul 06, 2012 at 09:52 PM 0
Share

Yeah I am positive Its executing the code and from the right script. It never changes it. $$anonymous$$y code is almost the exact same as what I posted above. I made the variable static and I could change it, but I rather not have to use a static variable.

avatar image Demigiant · Jul 06, 2012 at 11:30 PM 0
Share

I think you should post your exact code. The mix of pseudo-code you posted looks alright, as $$anonymous$$ike said, so something else must be going on.

avatar image daniel glenn · Jul 07, 2012 at 12:56 AM 1
Share

In the document I found this.

 public class ScriptB : $$anonymous$$onoBehaviour {
 
 public ScriptA a;
 
   void Start()
   {   
    a = gameObject.GetComponent<ScriptA>(); 
   }
 
   void Update() {
      if(space is pressed) {
            create object to follow;
            a.followBall = true;
       }
   }
 }

avatar image whydoidoit · Jul 07, 2012 at 10:02 AM 0
Share

That says both scripts must be attached to the same object - it would set them that way on startup.

avatar image Rhithik · Jul 07, 2012 at 02:46 PM 0
Share

That worked beautifully daniel glenn! Thank you so much.

avatar image
0

Answer by blenderblender · Dec 27, 2014 at 04:14 PM

 public GameObject MyGameObject;
 YourScript myScript;
 void Start()
 {
 myScript = MyGameObject.GetComponent<YourScriptName>();
 }
 void Update()
 {
 //for example  See is a bool in the script YourScriptName
 if(myScript.See==true)
 {
 // do Something
 }
 }
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 Snownebula · May 18, 2015 at 08:36 AM

How do you change the var in the js from the c# script?

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 sas_88 · May 18, 2015 at 09:11 AM

Hi,

@Snownebula

Follow the link: http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access

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

11 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

Related Questions

One script, different variable for each object. 1 Answer

Boolean is set to true but is unchecked in the inspector 1 Answer

Multiple Cars not working 1 Answer

C# Make array based on script variable 1 Answer

How do I check a boolean from another script in C#? 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