Button disappearing when I start playmode
I have a program where I am trying to have an array of sprites that I want to change with a button click. I have the sprites rendering, but when I enter Play mode the button object I have assigned to the Hood/Hair object is disappearing.
Can anyone help me out please?
images and code below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectionManager : MonoBehaviour {
public SpriteRenderer clothing;
public Button Select;
public Sprite[] clothes;
int num = 0;
void Start () {
clothing = GetComponent<SpriteRenderer> ();
Select = GetComponent<Button> ();
Select.onClick.AddListener(TaskOnClick);
}
void Update () {
clothing.sprite = clothes [num];
}
public void TaskOnClick ()
{
num = +num;
if (num >= clothes.Length)
num = 0;
}
}
postplay.png
(137.4 kB)
preplay.png
(116.5 kB)
Comment
Answer by $$anonymous$$ · Aug 02, 2017 at 06:31 PM
On line 15, you're assigning Select with a GetComponent(), but there's no Button component on the Hood/Hair GameObject. As a result, Select is set to null when you press play. If you get rid of that line it should work fine.