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][1] , [here][2] and [here][3] 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;
}
// Update is called once per frame
void Update () {
}
}
and this are my objects:
[![enter image description here][4]][4]
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?
[1]: https://answers.unity.com/questions/1127944/changing-the-image-of-a-button-with-a-script.html
[2]: https://stackoverflow.com/questions/30273435/unity-5-0-how-can-change-unity-ui-button-image
[3]: https://blog.gamedev.tv/how-to-add-a-background-image-to-the-ui-canvas-of-your-unity-game/
[4]: https://i.sstatic.net/rdWcr.png