I am trying to assign a texture to an already existing game object. Basically what I am trying to accomplish is two buttons, when one button is pressed it makes uses one texture, then when the other button is pressed it switches to the other texture.
This is basically simulating switching the picture on the menu on button press.
Below is the code I have. It compiles fine, but when I run it I get:
NullReferenceException: Object reference not set to an instance of an object
I’m assuming this is on the travelBoard. In unity I dragged the texture to the travelBoard reference. I thought this would create the reference. Do I actually have to setup the reference in the code (for example in the Start section)? If so how do I get just the texture? I tried .GetComponent(Texture) but that doesn’t seem to work either.
using UnityEngine;
public class ChangeTexture2 : MonoBehaviour
{
UITexture Board;
public Texture travelBoard;
void Start ()
{
}
void OnPress()
{
Board.mainTexture = travelBoard;
}
}