coroutine and gui.drawtexture

Hi all,
I’m having trouble with my code and I suspect it’s a problem with setting up a coroutine with GUI.DrawTexture .
I basically want some textures to appear after 2 seconds. I get a null reference exception problem: object not set to an instance of a object.

The following is the code:

using UnityEngine;
using System.Collections;

public class GameOver : MonoBehaviour {

	public Texture2D gameOverText;
	public float gameoverPosX;
	public float gameoverPosY;
	public float gameoverSclX;
	public float gameoverSclY;

	public Texture2D sideBar;
	public float sideBarPosX;
	public float sideBarPosY;
	public float sideBarSclX;
	public float sideBarSclY;

	public Texture2D siderBar;
	public float siderBarPosX;
	public float siderBarPosY;
	public float siderBarSclX;
	public float siderBarSclY;

	public Texture2D scoreTextDis;
	public float scoreTextDisPosX;
	public float scoreTextDisPosY;
	public float scoreTextDisSclX;
	public float scoreTextDisSclY;

	// Use this for initialization
	void Start () {
	
	}
	// Update is called once per frame
	void Update (){
		if (GameObject.Find ("GameAttribute").GetComponent<EarthKeepScore> ().gameOver == true) {
			StartCoroutine("GameOverGUI");	
		}
	}
	

	IEnumerator GameOverGUI(){

		yield return new WaitForSeconds (2);
		GUI.DrawTexture (new Rect (Screen.width * gameoverPosX, Screen.height * gameoverPosY, Screen.width * gameoverSclX, Screen.height * gameoverSclY), gameOverText);
		GUI.DrawTexture (new Rect (Screen.width * sideBarPosX, Screen.height * sideBarPosY, Screen.width * sideBarSclX, Screen.height * sideBarSclY), sideBar);
		GUI.DrawTexture (new Rect (Screen.width * siderBarPosX, Screen.height * siderBarPosY, Screen.width * siderBarSclX, Screen.height * siderBarSclY), siderBar);	
		GUI.DrawTexture (new Rect (Screen.width * scoreTextDisPosX, Screen.height * scoreTextDisPosY, Screen.width * scoreTextDisSclX, Screen.height * scoreTextDisSclY), scoreTextDis);
	}

}

I’m guessing this isn’t the correct way to do it although I don’t see why it shouldn’t work…

GUI.blah calls can only be made during OnGUI. My suggestion is to have a few flags in your GUI’s script and set those to true or false from your coroutines, and have the OnGUI() call draw things or not based on the flag.

Thank you Angry Penguin. I just set up a boolean to make it work and created OnGUI method. Very simple.
I thought that there must have been a way to make it work outside OnGUI.
Thanks once again