Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. We are looking for feedback on the naming of a new user research platform that we are working on.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Inspector Code Real Time Update?

Discussion in 'Scripting' started by Galactic_CakeYT, Sep 22, 2020.

  1. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    62
    So if you type a value in the Inspector it doesn't get hardcoded into your code. I was wondering if there was some code that I could make it so that when I type a value in the Inspector it gets saved to my code. Sorry if this a stupid question, but I generally don't know.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Inspector values are just serialized values. They don't alter the base code. For example, if you have public float myFloat = 5f; in your code and then set it to 10 in the inspector, it doesn't change the code it merely loads the serialized value and updates it when the code is executed.

    So think of inspector values as saved values. When you run your program, unity updates the variables in code with the saved ones.
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    1,751
    I believe OP knows that and wants to know if there is a way for the inspector to update the value written in the script file. In which case i believe the answer to be no. It does not make a whole lot of sense either, since a script may be attached to different objects, possibly containing different inspector values. So which of these would we update the script to? The value in the script would end up being that of the last updated object, which is not really helpful. It also has no practical usage, as the inspector value overwrites the default value set in the script anyways. And if it was not like that we could not have different inspector values on different objects.

    Edit: I understand the question tho as i was wishing for something similar when i started with Unity. In my case i always forgot that i needed to experiment with the inspector value and instead changed the base value in script.. and then wondered why nothing changed :p
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    1,498
    Is OnValidate what you're looking for?
    I don't really understand the question.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    9,770
    @Galactic_CakeYT It isn't possible to do what you want, but I recommend a different construct to help reduce value confusion.

    Instead of this:

    Code (csharp):
    1. public int MyInteger = 123;
    Do not put the 123 there.

    Instead put it in a Reset() method.

    Code (csharp):
    1. public int MyInteger;
    2.  
    3. void Reset()
    4. {
    5.   MyInteger = 123;
    6. }
    This Reset() method is called in the editor when you drag a script on, and can be called up from the little hamburger in the upper right window of the inspector for that script.

    This at least removes the temptation to change the initialization and then be confused why it didn't change the serialized value.
     
    eses and Yoreki like this.
  6. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    62
    Okay that's good to know that it changes automatically when building
     
  7. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    62
    When you make a [SerializedField] and edit the value in the Inspector. The new value doesn't go in the code, it is only in the Inspector. I was asking is there some code to make the Inspector value and the code value the same. Most people are saying that it isn't possible so...
     
  8. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    62

    Thanks, I'll try it.
     
  9. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    62

    Well I never thought of it like that :D
     
  10. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    1,498
    I'm still not sure what you mean, because changing a value in the inspector does change the value in the script, and
    OnValidate
    can show that:
    Code (CSharp):
    1. public class SomeClass : MonoBehaviour {
    2.    [SerializeField] private bool someBool;
    3.  
    4.    void OnValidate() {
    5.       Debug.Log(someBool);
    6.    }
    7. }
    If you were to add this script onto a GameObject and toggle
    someBool
    on/off in the inspector, it will log the correct value of true/false in the console respectively.
    OnValidate
    is called every time a change is made in the inspector.
     
  11. Galactic_CakeYT

    Galactic_CakeYT

    Joined:
    Apr 28, 2020
    Posts:
    62
unityunity