1

The script that I'm trying to run the function from looks like this:

public class switchAnimation : MonoBehaviour {

    public GameObject Character;
    private Animator myController;

    void Awake()
    {
        myController = Character.GetComponent<Animator>();
    }

    public void playPositive_large()
    {
        myController.SetTrigger("P_l");
    }
}

And I'm trying to run it from another script:

68: switchAnimation SA = GetComponent<switchAnimation>();
69: SA.playPositive_large();

But it gives me the following error:

"NullReferenceException: Object reference not set to an instance of an object Buttons.myAnims (UnityEngine.EventSystems.BaseEventData baseEvent) (at Assets/Main_A1/Buttons.cs:69) UnityEngine.Events.InvokableCall1[UnityEngine.EventSystems.BaseEventData].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:189) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent1[T0].Invoke (.T0 arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:53) UnityEngine.EventSystems.EventTrigger.Execute (EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:67) UnityEngine.EventSystems.EventTrigger.OnPointerEnter (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:73) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerEnterHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:22) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerEnterHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update() "

I tried different ways of calling the function but more or less I get the same error!

derHugo
  • 49,310
  • 9
  • 37
  • 73
Reza
  • 21
  • 3
  • Is your both script in the same gameObject? if it's not, then you have to get reference to your other gameObject, then call otherGameObject.GetComponent(); – endrik exe Jan 11 '18 at 02:59
  • No They're not. How can I get reference to the other gameObject?I tried using a gameObject but it also didn't work. – Reza Jan 11 '18 at 03:06
  • that's why you get NullReference, take a look at the duplicate question to find what is a NullReference. you will have to get reference to gameObject which has AnotherScript, and you doing it by creating a public variable like, `public GameObject otherGameObject;` and don't forget to attach your otherGameObject in the inspector, then you can call `otherGameObject.GetComponent();` – endrik exe Jan 11 '18 at 03:11
  • See the updated duplicate link. It shows how to fix this now. – Programmer Jan 11 '18 at 03:27

0 Answers0