Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Errors on creating Animation Script controller.

Discussion in 'Scripting' started by Picono435, Dec 25, 2019.

  1. Picono435

    Picono435

    Joined:
    Dec 24, 2019
    Posts:
    1
    Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class EnemyController : MonoBehaviour {
    7.  
    8.     private GameObject player;
    9.     NavMeshAgent agent;
    10.  
    11.     public float tempoParaTeleport;
    12.     bool podeTeleportar;
    13.     public GameObject gameC;
    14.     private Animator anim;
    15.     public bool estaTomandoDano;
    16.  
    17.     private GameObject canvas;
    18.  
    19.     void Awake()
    20.     {
    21.         player = GameObject.FindGameObjectWithTag("Player");
    22.         agent = this.gameObject.GetComponent<NavMeshAgent>();
    23.         podeTeleportar = true;
    24.         anim = GetComponentInChildren<Animator>();
    25.  
    26.         canvas = GameObject.Find("Canvas");
    27.  
    28.         estaTomandoDano = false;
    29.     }
    30.  
    31.     public void ChasePlayer()
    32.     {
    33.         Transform aux = player.GetComponent<Transform>();
    34.         float dist = Vector3.Distance(aux.position, transform.position);
    35.  
    36.         if (dist <= 3.0f)
    37.         {
    38.             agent.enabled = false;
    39.             anim.SetInteger("estado", 1);
    40.         }
    41.         else {
    42.             agent.enabled = true;
    43.             anim.SetInteger("estado", 1);
    44.             agent.destination = player.transform.position;
    45.         }
    46.     }
    47.        
    48.  
    49.     void Update()
    50.     {
    51.         Transform aux = player.GetComponent<Transform>();
    52.         float dist = Vector3.Distance(aux.position, transform.position);
    53.  
    54.         if(dist >= 60.3f )
    55.         {
    56.             Teleportar();
    57.             estaTomandoDano = false;
    58.         }
    59.  
    60.         else if(dist <= 30.0f)
    61.         {
    62.             ChasePlayer();
    63.             estaTomandoDano = false;
    64.         }
    65.  
    66.         if(dist <= 7.0f)
    67.         {
    68.             gameC.gameObject.GetComponent<GameController>().TomarDanoInimigo();
    69.             anim.SetInteger("estado", 2);
    70.             estaTomandoDano = true;
    71.         }
    72.  
    73.         PararDeAtacar();
    74.  
    75.         if(estaTomandoDano)
    76.         {
    77.             canvas.GetComponent<CanvasConfig>().IsDamagedImage();
    78.         }
    79.            else
    80.         {
    81.             canvas.GetComponent<CanvasConfig>().IsNotDamagedImage();
    82.         }
    83.  
    84.         Debug.Log("Esta tomando dano:"+estaTomandoDano.ToString());
    85.  
    86.     }
    87.  
    88.     IEnumerator CoolDownTeleport()
    89.     {
    90.         yield return new WaitForSeconds(tempoParaTeleport);
    91.         podeTeleportar = true;
    92.  
    93.     }
    94.  
    95.     public void Teleportar()
    96.     {
    97.         if (podeTeleportar)
    98.         {
    99.             podeTeleportar = false;
    100.             this.gameObject.transform.position = new Vector3(player.transform.position.x - 10,
    101.                 0f,
    102.                 player.transform.position.z - 10);//setar a position para proximo do player0
    103.             anim.SetInteger("estado", 0);
    104.         }
    105.  
    106.         else
    107.         {
    108.             StartCoroutine(CoolDownTeleport());
    109.             anim.SetInteger("estado", 0);
    110.         }
    111.  
    112.  
    113.     }
    114.  
    115.     void PararDeAtacar()
    116.     {
    117.         if (gameC.GetComponent<GameController>().vida <= 0)
    118.         {
    119.             anim.SetInteger("estado", 0);
    120.         }
    121.     }
    122.        
    123. }
    124.  
    Errors:

    NullReferenceException: Object reference not set to an instance of an object
    EnemyController.PararDeAtacar () (at Assets/Scripts/EnemyController.cs:117)
    EnemyController.Update () (at Assets/Scripts/EnemyController.cs:73)

    "SetDestination" can only be called on an active agent that has been placed on a NavMesh.
    UnityEngine.AI.NavMeshAgent:set_destination(Vector3)
    EnemyController:ChasePlayer() (at Assets/Scripts/EnemyController.cs:44)
    EnemyController:Update() (at Assets/Scripts/EnemyController.cs:62)


    If someone could help me...
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    "gameC" object doesn't have an instance at the time when this error happens. Maybe you forgot to reference it in the inspector or you set it to null at some point?

    Either that or it doesn't have the "GameController" component on itself.

    It can be either one of these.