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 uanmanarmy · Jul 15, 2014 at 08:37 AM · booleanreferenceaccessaccessing from any script

Variable not changing when you get a reference from another script

My problem is that When I want to get a variable from another script, it's accessing it but the value of it it's not changing.

 public class MenuScript : MonoBehaviour {
 
     public int newLevel ;
     void OnGUI()
     {
         GUI.Box (new Rect (10, 10, 100, 120), "Loader Menu");
 
         if(GUI.RepeatButton(new Rect(20,40,80,20), "Level1"))
         {
             newLevel = 1;
             
         }
 
         if(GUI.Button(new Rect(20,70, 80, 20), "Level2"))
         {
             newLevel = 2;print("Level " + newLevel);
             
         }
         if(GUI.Button(new Rect(20,100, 80,20), "Level3"))
         {
             newLevel = 3;
     
         }
     }
 }
  

  public class XMLParserScript : MonoBehaviour { 
   public GameObject myObject;
   public bool newLevel;

     void Start()
     {
         newLevel = false;
         objectScript = myObject.GetComponent <MenuScript>();
     }
 
     void Update () 
     {
                     
       LevelList (objectScript.newLevel, newLevel);
     }
     
     public void LevelList(int message, bool newLevel)
     {
         switch(message)
             {
             case 1:
             newLevel = true;
                 break;
             case 2:
             NewLevel = true;;
                 
             break;
                 
             case 3:
             newLevel = true;
                 break;
 
         default:
                 break;
                 
             }
     }

I want to access the newLevel Bool variable from XML Parser with another script.

 public class AutoPositionItems : MonoBehaviour {
     
 
     public XMLParserScript objectScript;        //Reference to the GemScript
     public GameObject myObject;        //Reference to the Gems Prefab
 
     void Start()
     {
         objectScript = myObject.GetComponent <XMLParserScript>();
         
     }
     void Update () 
     {
     
         
         if(objectScript.newLevel)
         {
             //dO SOMETHING
         }

            else 
            {
                    //do Something else
            }
     }
 }


The problem is that when I am pressing one of the GuiButton the newLevel boollean is switching to true and right after that it's switching back to false. Any idea why ?

Comment
Add comment · Show 6
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 Tehnique · Jul 15, 2014 at 08:49 AM 0
Share

I think you didn't post all of your code. The problem might be in some of the unposted code. Also, I think there's a problem with your logic, because once you click a button, the "newLevel" bool will forever be true. The switch back to false might be in your unposted code, or is this on purpose (can't imagine why)?

avatar image Fornoreason1000 · Jul 15, 2014 at 09:16 AM 0
Share

Hot Tip: use debug statements on each part where your "boolean" is refrenced for example

 void Start() {
 myBoolVar = myObject.SomeFunction();
 Debug.Log("Start... my bool is" + myBoolVar.ToString());
 
 }

this tells you where the bool is assigned... and the result of that function.... as soon as you see it set back to false in one of your function .. .then that is the function you work from to sort it out.

Honestly I do not know where the bool is being set to false, the only reason i can think of is Start is being called twice when you load into a new scene causing it to reset. but i can't see any DontDestryOnLoad functions or Sceneloading logic... post every piece of code that uses that boolean.

avatar image uanmanarmy · Jul 15, 2014 at 09:27 AM 0
Share

This is it, only this scripts are using the script.

I am not reloading any scene, I am getting data from an X$$anonymous$$L and I just update everything I have on my screen with new data.

It might be a problem if I have more than one Start function,

I mean 1 Start for each script?

avatar image Tehnique · Jul 15, 2014 at 09:40 AM 0
Share

No, that's normal, it's not that. I'm sure there are some lines you omited, for example, in the second script, objectScript is not a global variable, so it would never compile like this. Besides, there is no code that turns "newLevel" to false, appart from the code in the start function, and that's only executed once.

I might be missing something, but it's my hunch that there are lines you did not post, probably in order to make the code smaller and easier for us.

avatar image Fornoreason1000 · Jul 15, 2014 at 09:44 AM 0
Share

I don't see the point of more the one Start function per class... let alone see how the compiler would even let you do it... unless you are referring to my example? that's an example so yeah...

but have you tried the debug log statements?

 public class $$anonymous$$enuScript : $$anonymous$$onoBehaviour {
  
     public int newLevel ;
     void OnGUI()
     {
         GUI.Box (new Rect (10, 10, 100, 120), "Loader $$anonymous$$enu");
  
         if(GUI.RepeatButton(new Rect(20,40,80,20), "Level1"))
         {
             newLevel = 1;
  
         }
  
         if(GUI.Button(new Rect(20,70, 80, 20), "Level2"))
         {
             newLevel = 2;print("Level " + newLevel);
  
         }
         if(GUI.Button(new Rect(20,100, 80,20), "Level3"))
         {
             newLevel = 3;
  
         }
     }
 }
  
  
  public class X$$anonymous$$LParserScript : $$anonymous$$onoBehaviour { 
   public GameObject myObject;
   public bool newLevel;
  
     void Start()
     {
         newLevel = false;
         objectScript = myObject.GetComponent <$$anonymous$$enuScript>();
         print("Start has been called, newLevel is now false");
     }
  
     void Update () 
     {
  
       LevelList (objectScript.newLevel, newLevel);
       print(newLevel);
     }
  
     public void LevelList(int message, bool newLevel)
     {
         switch(message)
             {
             case 1:
             newLevel = true;
                 break;
             case 2:
             newLevel = true;;
  
             break;
  
             case 3:
             newLevel = true;
                 break;
  
         default:
                print(newLevel);
                 break;
  
             }
     }

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Tehnique · Jul 15, 2014 at 09:49 AM

Ok, I might have an idea what's wrong:

  void Update () 
     {
  
       LevelList (objectScript.newLevel, newLevel);
     }

In the code above you give newLevel as a parameter, even though it's defined as a member of the class you are in. That's a bad idea, because you don't actually work with the nelLevel from the class, but with a copy of it that is lost when the function ends.

The fix is simple and also a good practice:

Don't give any parameters to your LevelList function, because you alredy have them in the class where you work.

So something like this:

      public class XMLParserScript : MonoBehaviour { 
       public GameObject myObject;
       public bool newLevel;
       public MenuScript objectScript;
 //...

And then, in the Update:

  void Update () 
     {
  
       LevelList ();
     }

In the LevelList():

  public void LevelList()
     {
         switch(objectScript.newLevel)
             {
             case 1:
             newLevel = true;
                 break;
             case 2:
             newLevel = true;;
  
             break;
  
             case 3:
             newLevel = true;
                 break;
  
         default:
                 break;
  
             }
     }

Try it, I'm curious if that was the problem.

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 uanmanarmy · Jul 15, 2014 at 11:17 AM 0
Share

thanksa kkk

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

24 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

Related Questions

A node in a childnode? 1 Answer

Script not responding to public variable change 1 Answer

Simple Script getting Errors. 1 Answer

C# Boolean Doesn't Change Value 1 Answer

Access other variables please help :( 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