Search Unity

Question NullReference can't find GameObject (Animator)

Discussion in 'Animation' started by bergera, Nov 29, 2021.

  1. bergera

    bergera

    Joined:
    Nov 4, 2021
    Posts:
    1
    Hi Guys i currently have a Problem. I always get a NullReference when trying to get an Animator from my FBX file. I have placed an Object above it and to the Object is a Script attached and now i want to get the Animator of the FBX
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using System;
    6. using System.Net;
    7. using System.Net.Sockets;
    8. using System.Text;
    9.  
    10. namespace game
    11. {
    12.     public class PlayerMove : MonoBehaviour
    13.     {
    14.         public float moveSpeed = 3;
    15.         public float leftRightSpeed = 4;
    16.         public float jumpSpeed = 2;
    17.         public UDPSocket listener;
    18.         public bool playerJump;
    19.         Animator pAnimator;
    20.  
    21.         void Start()
    22.         {
    23.             listener = new UDPSocket();
    24.             listener.Server("255.255.255.255", 42070);
    25.             Console.ReadKey();
    26.             pAnimator = gameObject.GetComponentInChildren<Animator>();
    27.             playerJump = false;
    28.         }
    29.         void Update()
    30.         {
    31.             transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed, Space.World);
    32.             playerJump = false;
    33.  
    34.             if (Input.GetKey(KeyCode.W) || listener.Movement == "jump")
    35.             {
    36.                 if (this.gameObject.transform.position.x > LevelBoundary.leftSide)
    37.                 {
    38.                     transform.Translate(Vector2.up * Time.deltaTime * jumpSpeed);
    39.                     playerJump = true;
    40.                     pAnimator.SetBool("Jump", true);
    41.                 }
    42.             }
    43.             if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) || listener.Movement == "left")
    44.             {
    45.                 if (this.gameObject.transform.position.x > LevelBoundary.leftSide)
    46.                 {
    47.                     transform.Translate(Vector3.left * Time.deltaTime * leftRightSpeed);
    48.                 }
    49.             }
    50.  
    51.             if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) || listener.Movement == "right")
    52.             {
    53.                 if (this.gameObject.transform.position.x < LevelBoundary.rightSide)
    54.                 {
    55.                     transform.Translate(Vector3.right * Time.deltaTime * leftRightSpeed);
    56.                 }
    57.             }
    58.         }
    59.     }
    60. }
    61.  
     

    Attached Files: