Question by
TheSnipe789 · Jan 16, 2016 at 11:44 AM ·
c#uibuttonimage
Changing the image of a button with a script
Hi, I was unable to run a script to change the image of a button.
Script: (C#)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class Bouton123 : MonoBehaviour {
public Button button;
public Sprite Image1;
void Start () {
button = GetComponent<Button>();
button.image.sprite = Image1;
}
void Update () {
}
}
Error: "NullReferenceException: Object reference not set to an instance of an object"
Someone can help me ?
Comment
Best Answer
Answer by vintar · Jan 16, 2016 at 12:08 PM
you should say :
button.GetComponent<Image>().sprite = Image1;
Your answer
Follow this Question
Related Questions
Remove button OnMouseOver highlighting 0 Answers
Loading an 'UI skin pack' 1 Answer
Hotbar appears when a certain gameobject is selected? 1 Answer
UI buttons won't recognize script 0 Answers