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 AwesomeFaceHD · Sep 08, 2013 at 06:30 AM · c#inspectorbooleannot-working

Boolean is set to true but is unchecked in the inspector

I have a boolean called canLoad in my script. I set it to true in the code and in the inspector it's unchecked. I use it in the Menu_SP() method and when I start the game, it behaves how it should if it were false but it's set to true. Why is this?

 public class menuManager : MonoBehaviour {
 
     public string currentMenu;
 
     public bool canLoad = true;
 
     public GUIStyle menuBG;
     public GUIStyle Labels;
     public GUIStyle Buttons;
     public GUIStyle smallButtons;
     public GUIStyle notYetBttn;
     public GUIStyle Text;
     public GUIStyle smallText;
 
     void Start () {
         currentMenu = "Main";
     }
     
     void Update () {
         
     }
 
     void OnGUI() {
         if(currentMenu == "Main")
             Menu_Main();
         if(currentMenu == "SP")
             Menu_SP();
         if(currentMenu == "Opt")
             Menu_Opt();
     }
 
     public void NavigateTo(string nextmenu) {
         currentMenu = nextmenu;
     }
 
     public void Menu_Main() {
         GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "", menuBG);
         GUI.Label(new Rect(400, 50, 300, 50), "IDGAF", Labels);
         if(GUI.Button(new Rect(400, 150, 300, 50), "Singleplayer", Buttons)) {
             NavigateTo("SP");
         }
         if(GUI.Button(new Rect(400, 210, 300, 50), "Multiplayer", notYetBttn)) {
 
         }
         if(GUI.Button(new Rect(400, 270, 300, 50), "Options", notYetBttn)) {
             
         }
         if(GUI.Button(new Rect(400, 330, 300, 50), "Exit", Buttons)) {
             Application.Quit();
         }
     }
 
     public void Menu_SP() {
         GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "", menuBG);
         GUI.Label(new Rect(400, 50, 300, 50), "Singleplayer", Labels);
         if(GUI.Button(new Rect(400, 150, 300, 50), "New Game", Buttons)) {
             
         }
         if(canLoad == true){
             if(GUI.Button(new Rect(400, 210, 300, 50), "Load Game", Buttons)) {
             
             }
         }
         else if(canLoad == false) {
             if(GUI.Button(new Rect(400, 210, 300, 50), "Load Game", notYetBttn)) {
 
             }
         }
         if(GUI.Button(new Rect(400, 270, 300, 50), "Back", Buttons)) {
             NavigateTo("Main");
         }
     }
 
     public void Menu_Opt() {
 
     }
 }
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 fafase · Sep 08, 2013 at 06:33 AM

Because public variables only get their value from the script on the first time. You may have modified that variable from inspector and this value is now the chosen one. You can see that modifying the value in the script will not change it in the inspector.

The value in script is now "useless".

You would have to click Reset in the top right drop down of the script. This would set the value to the one in the script.

Only private variables get their value from script.

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 AwesomeFaceHD · Sep 08, 2013 at 06:42 AM 0
Share

Ahh, thank you. Can I still access a private variable from another script?

avatar image fafase · Sep 08, 2013 at 06:44 AM 0
Share

No. But you can create a public method or a property:

 private int member;
 public int Get$$anonymous$$ember(){
    return member;
 }
 public void Set$$anonymous$$ember(int member){
    this.member = member;
 }

or

 private int member;
 public int $$anonymous$$ember{
    get{return member;}
    set{member = value;}
 }
avatar image AwesomeFaceHD · Sep 08, 2013 at 06:46 AM 0
Share

Okay, thank you for the help!

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

15 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

Related Questions

Distribute terrain in zones 3 Answers

Inspector cannot find script instance 1 Answer

Cannot assign assets to variables via inspector: NullReferenceException 3 Answers

Inspector vs Script: Component best practice? 1 Answer

Multiple Cars not working 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