Once instantiated, scripts inside a prefab dont initialize until next frame

Is there a way to manually tell a script to instantiate? I need to access some variables in the newly spawned script the same frame it is created.

Scripts are not spawned without a game object, they are added to a game object or they are instantiated together with a prefab being instantiated.

Important note: If you need to access variables from these scripts that are set in Start, that won’t work. You need to set variables during Awake in those scripts to access them as soon as they have been instantiated or added to a game object.

Example Script used in the following use-cases:

public class Script : MonoBehaviour {
    public string someValue;
    void Awake() {
        someValue = "This was set by the script!";
    }
}

Adding a script to your own gameObject:

void Start() {
    var script = gameObject.AddComponent<Script>();
    print(script.someValue);
}

Adding a script to a newly created game object:

void Start() {
    var gameObject = new GameObject("Newly Created GameObject");
    var script = gameObject.AddComponent<Script>();
    print(script.someValue);
}

Adding a script to another existing game object:

void Start() {
    var gameObject = GameObject.Find("Player");
    if (gameObject) {
        var script = gameObject.AddComponent<Script>();
        print(script.someValue);
    }
}

Instantiating a game object and accessing it’s script:

public GameObject prefab;

void Start() {
    if (prefab) {
        var gameObject = (GameObject)Instantiate(prefab);
        var script = gameObject.GetComponent<Script>();
        if (script)
            print(script.someValue);
    }
}

Instantiating a game object equipped with a script and accessing it’s script:

public Script prefab;

void Start() {
    if (prefab) {
        var script = (Script)Instantiate(prefab);
        print(script.someValue);
    }
}