Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Public variable controlled in inspector

Discussion in 'Scripting' started by phoda, Jan 22, 2016.

  1. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    I dont know how i didnt encounter this problem yet but public variables cant be changed in script once they are set and need to be changed in inspector. [HideInInspector] doesnt help. Seems like stupid question to me but bothers me.
    Thanks.
     
  2. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    You should be able to modify a public variable from a script. I believe they are initialized based on the inspector set value. Do you have a code example?

    Something like this should work fine:

    Code (CSharp):
    1. public int _myInt;
    2.  
    3. void Update()
    4. {
    5.     if (Input.GetKeyDown(Keycode.Space))
    6.     {
    7.         _myInt++;
    8.     }
    9. }
     
  3. Dennis_eA

    Dennis_eA

    Joined:
    Jan 17, 2011
    Posts:
    375
    I think what you want is: Reset.

    on the top right of your inspector (where your script is attached) is a tiny wheel symbol, click it and RESET.
    This resets all variables there.. so another trick would be to change your variable inside your code to private, set new value (in script), safe, switch to unity, switch back to code and undo.
     
    mmercer8 likes this.
  4. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Nope Reset is not what i want as that removes and all references to sprites and such.

    I set my public int in initialization part of script. It can be changed during run time but i cant change starting values in script. So i just ask is there way to hide AND DISABLE value in inspector. It needs to be public so others script can access it and i dont like changing starting values in inspector.

    Thanks
     
  5. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Yes value starts as
    Code (CSharp):
    1. public float maxSpeed = 10f;
    and since I'am testing some values changing value to
    Code (CSharp):
    1. public float maxSpeed = 0f;
    does nothing, i have to go to inspector and change it there.

    Its not something that is MUST but frustrating little thing. So if someone knows other way. Thanks
     
  6. Dennis_eA

    Dennis_eA

    Joined:
    Jan 17, 2011
    Posts:
    375
    Atleast when using UnityScript I am 99.9975% sure that

    @HideInInspector public var someVarHidden : float = 1.234;

    works
     
  7. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384

    Iam using C# and nope [HideInInspector] just hides value in inspector. Changing variable in initialization still doesnt change its value at start but thanks for help.

    I moved setting variable in start function but if someone knows way please reply.
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. public class TestScript : MonoBehaviour
    3. {
    4.     [HideInInspector]
    5.     public float myFloat = 50f;
    6.  
    7.     void Start()
    8.     {
    9.         Debug.Log(myFloat);
    10.     }
    11. }
    12.  
    float was originally set to "= 10f", ran game, output "10"
    change to the above, output "10"

    the inspector is being set to 10, once done it overrides any changes to the value in the script in subsequent runs. So it'll work in the first play, after that it is as described by @phoda
     
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    there doesn't appear to be a way to reset a single field, it looks to be the entire component, or nothing o_O:(
     
  10. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    exactly. I just ask is there way to "detach" variable from inspector and assign it to change in script.

    no idea how i didnt encounter this before
    [!Inspector] function would be nice addition, Unity
     
  11. Dennis_eA

    Dennis_eA

    Joined:
    Jan 17, 2011
    Posts:
    375
    how about a Variable-script that uses only private variables, but from inside that script passes all it's vars to the other scripts, where those are public (of course) and hidden in inspector.

    The "variable-script" passes it's values to the other scripts with a bunch of get components inside start-function
     
  12. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    I did something like that, I initalized public floats and then set their values in start function so now changing them in start function works. Thought about one script for variables but its really small game so no need
     
  13. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    If you just don't need them to show in the inspector and you want to be able to change them from other scripts, good practice is:

    Code (CSharp):
    1. private float _MyInt = 10.0f;
    2. public float MyInt
    3. {
    4.     get { return _MyInt; }
    5.     set { _MyInt = value; }
    6. }
    Then you can access "MyInt" from any other script.
     
  14. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Comment out the line in question. Run in Unity. Uncomment the line.
     
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Mark them as nonserialised. Unity will then ignore them.
     
  16. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    As BoredMormon said, add [System.NonSerialized] to them.
     
    Kiwasi likes this.
  17. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Explanation is needed:

    [HideInInspector] works exactly like a public variable, but you can't see it in the inspector. The value is still saved to file.
    [NonSerialized] makes Unity not store any information about the variable. It will get the default value as the script object is constructed.
     
  18. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Wow i think this is it. But will be variable then saved for save/load as it is serializing variables and this one is nonserialized?
     
  19. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    what
     
  20. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    hahaha im sorry i thought it prevented [serializable] in class for that varaible but i realized, its stupid question sorry, but thanks for help
     
  21. i-am-developer-9

    i-am-developer-9

    Joined:
    Jul 29, 2020
    Posts:
    5
    Thanks it works for me