0
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);
}
}
}
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.– Maniero
On line 54 if(playerHealth.currentHealth > 0)
– Daniel Correa Santos
Good thing you posted the content, because we also don’t have the line number. You called the method
Awake()
before executing theAttack()
? is in this method that the fieldplayerHealth
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– Maniero
No, it is the opposite. Even by the names, it makes sense that first wake up and then attack.
– Maniero
So what do I do?
– Daniel Correa Santos
@bigown The method
Awake()
is automatically called by Unity-3d after the object is created, always before theUpdate()
. 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.– Victor Stafusa
@Victorstafusa good to know, by the way is not being called or some error that passes beaten is occurring in this call.
– Maniero
I reversed the position of the code but no good look
– Daniel Correa Santos
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?– Victor Stafusa
file://C:/Users/user/Desktop/Capture.PNG
– Daniel Correa Santos
It’s how the print passes the comment
– Daniel Correa Santos
I’m sure just don’t know how to print I’m new here
– Daniel Correa Santos
@Danielcorreasantos Edit the question is to include the cousin there (there is a button for this in the editor).
– bfavaretto
Is developing in 2D or 3D?
– MeuChapeu
If you’re making this mistake on this line, it’s because the variable
playerHealth
is void. And this is due to the callplayerHealth = 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").– Luiz Vieira
unity3d @Meuchapeu
– Daniel Correa Santos