- Home /
Unable to change the color of a button on a disabled button.
Hi there. I am trying to change the color of a button that is disabled on a panel. When I click on the button to change the color, I get a null reference error. When I click on another button to enable the panel that is disabled with the button on it that is also disabled as a child, I see no change in the color settings. I have tried multiple different ways to make the color set on an image of a button on a disabled button.
These are my two methods I currently have that are not functioning correctly
Method (1). Making the gameobject locate the script and set its values
public void ColorToSetBtnPressed2()
{
if (selbtn2.activeSelf) {
CurCol2.GetComponent<ColorSet2>().CurCol2.GetComponent<Image>().color = ColorToSet2;
}
}
Method (2): Adding in a Resources.FindObjectsOfTypeAll function to force the button to show the changes to the image color.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class FindAllGameObjects : MonoBehaviour {
// this script locates all the gameobjects in the current scene
public static List<GameObject> FindAllObjectsInScene()
{
UnityEngine.SceneManagement.Scene activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene ();
GameObject[] rootObjects = activeScene.GetRootGameObjects ();
GameObject[] allObjects = Resources.FindObjectsOfTypeAll<GameObject> ();
List<GameObject> objectsInScene = new List<GameObject> ();
for (int i = 0; i < rootObjects.Length; i++) {
objectsInScene.Add (rootObjects [i]);
}
for (int i = 0; i < allObjects.Length; i++)
{
if (allObjects[i].transform.root)
{
for (int i2 = 0; i2 <rootObjects.Length; i2++)
{
if (allObjects[i].transform.root == rootObjects[i2].transform && allObjects[i] != rootObjects[i2])
{
objectsInScene.Add (allObjects [i]);
break;
}
}
}
}
return objectsInScene;
}
}
Any help would be very helpful.
Your answer
Follow this Question
Related Questions
GameObject keeps acting like gameObject. 1 Answer
Button animation state stays Pressed or Highlighted after disabling GameObject 0 Answers
UI Button color 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer