Script Order

I have a GameManager that holds a list of sprites. It loads the list using Resources.LoadAll(); Then, I have and objective script that assigns those sprites to image objects.

Objective script looks like this:

public void SetObjective ()
        int rand = (int)Random.Range (0.0f, GameManager.manager.gameItems.Length);
        Debug.Log (GameManager.manager.gameItems[1].name);
        this.GetComponent<Image> ().sprite = GameManager.manager.gameItems[1];

This exact same code works to create a grid of sprites. The debug log occurs after the NullRefernceException error, so I changed the script execution order and got the same results. What else can I try changing to get the script to run at a correct time after all of the images have been loaded?

I try not to mess with script execution order, so I dont know much about that.

I would suggest loading what you need to load in “public void Awake(){}” then do the rest of your work in “public void Start(){}” All Awake() functions are run before all the Start() functions, so you should have your references set up by the time it gets to Start().

Another option would be to call your SetObjective() function at the end of your original script which loads all the resources.