Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Feref2 · Jan 01, 2021 at 09:26 PM · variablescallingoverwrite

Calling a variable from another script overwrites it?

Am just doing that. I have a bool: [HideInInspector] public bool exampleBool = true; Originally It was only true if it was marked in the inspector, but I changed it to this so it would always be true. Anyway, am calling it from its own script several times just to debug and is always true, as it should. But am also calling it from the other script and is always false. In fact, am not even changing its value. I know that the problem is that the second script overwrites the true value to false becase I created this:

public void testValue() { print("exampleBool: " + exampleBool); }

Now, this is the thing: am calling this void from both its own script and another one, both in Update(); The result from its own is true (as it should), but the other is false.

So this is why am assuming that calling the variable from other script overwrites it, but I don´t know why.

Comment
Add comment · Show 8
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 sacredgeometry · Jan 01, 2021 at 09:33 PM 0
Share

So a few things:

  • If your bool is always true then it probably doesn't need to exist.

  • If it needs to be configurable but constant make it a constant.

  • Bools are structs and have a default value and by default a bool is false if you are accessing it before its explicitly set then it will be false. So either set it to true in its declaration or make sure you are accessing it after it is set.

avatar image Feref2 sacredgeometry · Jan 01, 2021 at 09:39 PM 0
Share

It´s not always true. That was my way of confir$$anonymous$$g that the other scropt was overwriting it. Its not constant. The deafult value is false, but as I wrote, it´s set to true in its declaration: [HideInInspector] public bool exampleBool = true; Am also setting it to true in Awake() just to be completely sure, then accesing it from Update().

avatar image sacredgeometry Feref2 · Jan 01, 2021 at 09:43 PM 0
Share

The HideInInspector attribute does not remove the cached serialisation on fields.

$$anonymous$$akes a variable not show up in the inspector but be serialized.

There is a chance that it is set to false in the inspector which would be overriding you setting it in the initialisation. It shouldnt be overriding the one in your awake though. But if you dont need it to be serialised and want to have a variable that isnt visible in the inspector why not use a property instead?

avatar image sacredgeometry sacredgeometry · Jan 01, 2021 at 09:45 PM 0
Share

How are you referencing the GameObject/ method in the other script?

avatar image Feref2 sacredgeometry · Jan 01, 2021 at 10:59 PM 0
Share

public GameObject[] rigidBodies; $$anonymous$$eshInfo[] info;

 void Start()
 {
     info = new $$anonymous$$eshInfo[rigidBodies.Length];
     for (int i = 0; i < rigidBodies.Length; i++)
     {
         info[i] = rigidBodies[i].GetComponent<$$anonymous$$eshInfo>();
         print("bool: " + info[i].exampleBool);//stays false
     }
 }

Am not using properties because I don´t know them... What are those?

avatar image unity_HTr7mOeTIRzvVA · Jan 01, 2021 at 11:42 PM 0
Share

Don't let other script modify the value but still can read the value. public bool exampleBool { get; private set; } void Start(){ exampleBool = true; }

avatar image Feref2 unity_HTr7mOeTIRzvVA · Jan 02, 2021 at 12:56 AM 1
Share

Thanks a lot. It works! But I have a question: If I were to want to modify the value from the second script, what should I do? Because this unables any possible modification. Right now is not my need, but it might be useful in the future.

avatar image unity_HTr7mOeTIRzvVA Feref2 · Jan 02, 2021 at 03:17 AM 0
Share

To modify the value from other script, you can make separate methods or get set accessors

 public bool exampleBool{ get; private set; }
 
 public bool ExampleBool{ set{ exampleBool = value; }}
 
 
 // or
 
 
 public void SetExampleBool(bool newValue){ exampleBool = newValue; }
 
 
 // To other scripts
 
 
 public ScriptThatHasExampleBoolOnIt scriptThatHasExampleBoolOnIt;
 
 void Start(){
      scriptThatHasExampleBoolOnIt.ExampleBool = true;
 
     // or
 
      scriptThatHasExampleBoolOnIt.SetExampleBool(true);
 }


https://www.youtube.com/watch?v=HzIqrlSbjjU

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ludovicb1239 · Jan 01, 2021 at 09:51 PM

The first script is correct. I would just do in Start() { exampleBool = true;} and in the second script in Update(){ Debug.Log(FirstScript.exampleBool); } Hope it helps a bit

Comment
Add comment · Show 1 · 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 Feref2 · Jan 01, 2021 at 11:06 PM 0
Share

Your help is appreciated, but it doesn´t really work. I´ve been debuging it in Start() and Update() of the second script and is always false. I tried the same with the original script and it shows true always, so for some reason calling it from the other script overwrites it.

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

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

Related Questions

How do you return variables from other scripts? 2 Answers

Issue calling a variable from another script - C# 1 Answer

how do i call for a string variable from another script? 1 Answer

Calling variables from other scripts 2 Answers

Change transform variable from outside 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