0

Есть класс player в котором находится функции остановки(stopPlayerAnim) и запуска(startPlayerAnim) анимации игрока, и класс Joystic, в котором они запускаются. Но когда я жму на джойстик игра вылетает со следующей ошибкой:

NullReferenceException: Object reference not set to an instance of an object player.startPlayerAnim () (at Assets/scripts/player.cs:46) Joystic.OnDrag (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/scripts/Joystic.cs:13) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDragHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:71) 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:262) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)>

При этом ошибка ссылается на эту строчку: player1.GetComponent<Animator>().SetBool("ball_anim", true);

Из-за чего она появляется, и как её можно устранить? Код вышеупомянутых скриптов находится ниже:

player.cs:

public class player : MonoBehaviour
{
public static GameObject player1;

void Start()
{

}

public static void startPlayerAnim()
{

    player1.GetComponent&lt;Animator&gt;().SetBool(&quot;ball_anim&quot;, true);
    player1.GetComponent&lt;Animator&gt;().SetBool(&quot;inverted_ball_anim&quot;, false);
}

public static void stopPlayerAnim()
{

    player1.GetComponent&lt;Animator&gt;().SetBool(&quot;ball_anim&quot;, false);
    player1.GetComponent&lt;Animator&gt;().SetBool(&quot;inverted_ball_anim&quot;, false);
}

public static void startInvertedPlayerAnim()
{
    player1.GetComponent&lt;Animator&gt;().SetBool(&quot;inverted_ball_anim&quot;, true);
    player1.GetComponent&lt;Animator&gt;().SetBool(&quot;ball_anim&quot;, false);

}









}

gil9red
  • 77,085
  • 1
    Проверяли значения player1 и player1.GetComponent<Animator>()? Наверняка одно из них будет null. Кст, а где инициализация player1? – gil9red Feb 16 '22 at 14:13

1 Answers1

1
  1. Вы не инициализировали player1, у вас просто пустое поле.

  2. Также проверьте, есть ли скрипт аниматора на объекте, у которого вы пытаетесь получить компонент.

KOTlK
  • 672