GUI.Drawtexture "nullreferenceexception"

Hey all,

I have a simple ray cast script that when i click on an object and it is a poster, it should show a full screen image, of that poster.

This is my code so far and it is coming up with some strange instance error i dont get

Most of the code is copied from the unity GUI.Drawtexture example, i have just added raycasthit detection and tag filtering.

The error is:

NullReferenceException: Object reference not set to an instance of an object

var aTexture : Texture;

function Update() {
var hit: RaycastHit;
var ray = Camera.main.ViewportPointToRay(new Vector3(0.5, 0.5, 0.0));



	if (Input.GetButtonUp("Fire1")) {

			
			if (Physics.Raycast(ray, hit)) {

			
			 if (hit.transform.tag == "poster")
			 {
			 Debug.Log("A POSTER");
			 Debug.Log(hit.transform.gameObject.name);

			 
			 GUI.DrawTexture(Rect(10,10,60,60), aTexture, ScaleMode.ScaleToFit, true, 10.0f);
			 
			 }
			else{ Debug.Log("NOT A POSTER"); }
					
			}
		}
	}

You haven’t set the value of aTexture anywhere. If you the texture of the gameObject the RayCast hit, then you will have to use aTexture = hit.transform.renderer.sharedMaterial.mainTexture to assign your texture.

var aTexture : Texture; created a variable in the inspector to select a texture.

I noticed the main line

GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), aTexture, ScaleMode.ScaleToFit, true);

it does not error in its own function, but if it is in the update function it shows the instance error.

perhaps I can put it into its own function and call it on a raycast hit?