Awake and Start not called for script

by tmighty   Last Updated July 12, 2019 01:13 AM

I have a complex scenario which I have reduced to the following problem:

I have a Cube in my Unity scene. This cube has the following script on it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainScript : MonoBehaviour
{
    public OtherScript TheOtherScript;

    public void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 50, 50), "Load something"))
        {
            this.TheOtherScript.LoadFile();
        }
    }
}

The script "OtherScript" looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OtherScript : MonoBehaviour
{
    private string _sPath;

    public void Awake()
    {
        _sPath = Application.persistentDataPath + "/somefilename.dat";
    }
    public void Start()
    {
        _sPath = Application.persistentDataPath + "/somefilename.dat";
    }

    public void LoadFile()
    {
        if (_sPath == null)
        {
            Debug.Break();
        }
        //do something with _sPath
    }
}

I have created an empty GameObject to my scene, and I have added such a script to it:

enter image description here

I have then put a reference to empty GameObject to the Cube's "OtherScript" slot:

enter image description here

The problem that I'm experiencing is that "TheOtherScript"'s start / awake has not been called when this button is pressed:

    if (GUI.Button(new Rect(0, 0, 50, 50), "Load something"))
    {
        this.TheOtherScript.LoadFile();
    }

Neither Awake nor Start is called on this script.

Which event could I react to in this case for initialization?

Tags : unity c#


Related Questions


Updated July 02, 2019 11:13 AM

Updated July 12, 2015 13:05 PM

Updated July 13, 2015 13:05 PM