Loading order between values in inspector and values set in Start or Awake

I was hoping to set some default values for variables in a component, and override some of them in the editor from time to time for experimentation. At what stage in the lifecycle of a script can I put in values that will be read in before the values in the editor are read in ?

I believe any public objects that have their data changeable in the inspector are actually created and loaded before your scripts ever run. So Awake or Start should be fine for overriding them.
This code will print 6 no matter what inspector says:

using UnityEngine;
using System.Collections;

public class ExampleTest : MonoBehaviour
{
    public int testInt;

    private void Awake()
    {
        testInt = 6;
    }

    void Start()
    {
        Debug.Log("TestInt = " + testInt);
    }
}

Note however, the inspector does override implicit declarations. If you set testInt to 40 in the inspector it will print out 40 no matter what value you put in the script in this example:

using UnityEngine;
using System.Collections;

public class ExampleTest : MonoBehaviour
{
    public int testInt = 10;

    void Start()
    {
        Debug.Log("TestInt = " + testInt);
    }
}
1 Like

In the Scope and Access modifiers tutorial starting at about 1:15 it goes over this.

2 Likes

Thanks both. Takatok, that answered exactly what I needed to know :slight_smile:

I believe this is because the order of operations is essentially this: Allocate, Constructor, Deserialize, Awake. The declaration assignments are essentially part of the constructor, so they get set before the inspector values get deserialized. The same thing would happen if you were to write an explicit constructor that assigned 40 to the field.

1 Like

Is there any way to get those initial values to appear in the Inspector? I mean before running the game? So I only need to tweak that ones that really need changing? As it is, the inspector appears to set them to 0 overriding what is in the initial values forcing me to fill them in. Whereas if I set them in Start is overrides the ones in the Inspector. Seems there isn’t a compromise.

They always show up in the inspector for me. But only if I have them specified in code before adding the script to a game object in the editor. Any scripts that have already been instantiated in the editor will already have their values stored, requiring you to manually override them. Any new script instances of that script type added to game objects will default to the values specified in code. If that’s not what you’re seeing, it sounds like something unusual is happening.

Oh I already have the components added and have been changing the code with them already added… It would be annoying to have to delete and readd them. Ok I’ll do without this. I can see why it doesn’t update the inspector restrospectivily as that might overwrite values you had deliberately edited in the Inspector. I think what maybe might be a nice feature is if there was a toggle by each field to enable inspector value or use script value. But not all that important.

If you’ve already added the field and changed the field initialization value in the script afterwards, you can always reset the component in the inspector. But that would reset all the values of the script unless you customize it.

2 Likes

Thanks Suddoha, I hadn’t noticed the Reset option! That does make things easier :slight_smile: