I went to make a game in Unity and gave the error Nullreferenceexception

Asked

Viewed 190 times

0

inserir a descrição da imagem aquiThe code is this:

using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour
{
    public float timeBetweenAttacks = 0.5f;
    public int attackDamage = 10;


    Animator anim;
    GameObject player;
    PlayerHealth playerHealth;
    //EnemyHealth enemyHealth;
    bool playerInRange;
    float timer;


    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player");
        playerHealth = player.GetComponent <PlayerHealth> ();
        //enemyHealth = GetComponent<EnemyHealth>();
        anim = GetComponent <Animator> ();
    }


    void OnTriggerEnter (Collider other)
    {
        if(other.gameObject == player)
        {
            playerInRange = true;
        }
    }


    void OnTriggerExit (Collider other)
    {
        if(other.gameObject == player)
        {
            playerInRange = false;
        }
    }


    void Update ()
    {
        timer += Time.deltaTime;

        if(timer >= timeBetweenAttacks && playerInRange/* && enemyHealth.currentHealth > 0*/)
        {
            Attack ();
        }

        if(playerHealth.currentHealth <= 0)
        {
            anim.SetTrigger ("PlayerDead");
        }
    }


    void Attack ()
    {
        timer = 0f;

        if(playerHealth.currentHealth > 0)
        {
            playerHealth.TakeDamage (attackDamage);
        }
    }
}
  • 1

    Where did the error come from? Give details. Usually this error occurs by a problem originating elsewhere or you have not dealt with a situation that was certain that could have an object with value null. If you do not know how to do this, you will have enormous difficulty dealing with other aspects of the code.

  • On line 54 if(playerHealth.currentHealth > 0)

  • 1

    Good thing you posted the content, because we also don’t have the line number. You called the method Awake() before executing the Attack()? is in this method that the field playerHealth is being initialized. Without its initialization, it will give this same error. Anyway, it seems that this class is having problems of design in general. http://answall.com/q/73530/101

  • No, it is the opposite. Even by the names, it makes sense that first wake up and then attack.

  • So what do I do?

  • @bigown The method Awake() is automatically called by Unity-3d after the object is created, always before the Update(). The design is not the best, but this design is the shape that is imposed by Unity-3d and is the standard design of Unity-3d.

  • @Victorstafusa good to know, by the way is not being called or some error that passes beaten is occurring in this call.

  • I reversed the position of the code but no good look

  • 2

    A very simple question: Are you sure you put an object of the type PlayerHealth in your player? If yes, could post a print of the Inspector with this?

  • file://C:/Users/user/Desktop/Capture.PNG

  • It’s how the print passes the comment

  • I’m sure just don’t know how to print I’m new here

  • @Danielcorreasantos Edit the question is to include the cousin there (there is a button for this in the editor).

  • Is developing in 2D or 3D?

  • 1

    If you’re making this mistake on this line, it’s because the variable playerHealth is void. And this is due to the call playerHealth = player.GetComponent <PlayerHealth> (); fail. The only conclusion is what colleague @Victorstafusa has already mentioned: you have not added the object of this type to the player object (the tag "Player").

  • unity3d @Meuchapeu

Show 11 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.