I just wanted to make the text go from white to gold and I found a tutorial that showed how to change a button image doing so, so I figured if I change image to text in the getcomponent section it would get the text component. And it did let me add it to the canvas, thus I added it to the text to which I had added a button component to previously. However! When I did a test run I got this error:
NullReferenceException: Object reference not set to an instance of an object
AthleticsColor.changeColor () (at Assets/AthleticsColor.cs:12)
UnityEngine.Events.InvokableCall.Invoke () (at <ba783288ca164d3099898a8819fcec1c>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <ba783288ca164d3099898a8819fcec1c>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:514)
And the code in question:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AthleticsColor : MonoBehaviour
{
public GameObject athleticsText;
public void changeColor()
{
athleticsText.GetComponent<Text>().color = new Color(231,179,20);
}
}
I feel like I’m so close to getting it but I’m hitting a wall here. I’m not getting any compiler errors outside of running it and I tried changing the class and the file name to match the gameobject and it told me not to do that so I undid that. Anybody have advice?