- Home /
How does this UI toggle work?
Hi there, thanks for trying to help!
I'm trying to make a toggle button for an rts like game that toggles hold fire. When the button is pressed a function in the script that holds the selection array is called, wich is HoldFireToggle(), it then sends a message to the AI unit, this is what I have:
//this is in selection script
function HoldFireToggle ()
{
selected[fixedSelectedLength].SendMessage("HoldFire");
}
//this is what the AI unit does
function HoldFire ()
{
if(holdFire)
holdFire = false;
else
holdFire = true;
Debug.Log("function called and set to: " + holdFire);
}
but when this is used I get a big error: NullReferenceException: Object reference not set to an instance of an object selection.HoldFire () (at Assets/Scripts/selection.js:123) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:149) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:626) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:766) UnityEngine.Events.UnityEvent`1[System.Boolean].Invoke (Boolean arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:54) UnityEngine.UI.Toggle.Set (Boolean value, Boolean sendCallback) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:184) UnityEngine.UI.Toggle.Set (Boolean value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:159) UnityEngine.UI.Toggle.set_isOn (Boolean value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:153) UnityEngine.UI.Toggle.InternalToggle () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:216) UnityEngine.UI.Toggle.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Toggle.cs:227) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Answer by LucasVmarrewyk · Oct 21, 2015 at 03:35 PM
Fixed it, forgot to make a for loop and change the selected[fixedSelectedLength] to the var in the for loop