Sorry if this question has already an answer somewhere, but I've just started to play with Unity and now I'm stuck. :(
I've tried a few things ( from here , here and here and others ) but none of them work, I'm sure that I do something wrong but I don't know what.
I have this code:
public class SceneManager : MonoBehaviour {
public Image myButtonImage;
private Button switchBtn;
// Use this for initialization
void Start () {
switchBtn = GameObject.Find("switchBtn").GetComponent<Button>();
myButtonImage = Resources.Load<Image>("switchCameraImg");
switchBtn.image = myButtonImage;
}
}
and this are my objects:
where cameraSwitchObj
is a GameObject
and switchBtn
is the button itself.
SceneManager
is the script attached to MainCamera
I receive this error :
NullReferenceException: Object reference not set to an instance of an object SceneManager.Start ()
for this line : switchBtn.image = myButtonImage;
but according to what i read from different posts this is all i must do. What I am missing?