- Home /
Duplicate Question
How to check collision with specific object
Hi, I'm having trouble with my health script for my EnemyAI script, the issue is basically that if my enemy collides with anything and a boolean of a player script called 'IsAttacking' is set to true, I can just attack until they're dead without touching them. Can anyone give me a hint on how to fix this? Here's my script:
var Health : float = 10000;
var Self : GameObject;
var PlayerScript : PlayerScript;
function OnTriggerStay () {
if(PlayerScript.IsAttacking == true){
Health -=5;
if(Health == 0){
Destroy (Self);
}
}
//Nothing
}
Answer by whydoidoit · May 11, 2013 at 07:30 AM
EDIT: Thought you were using a static due to your naming convention! (I must be slow this morning)
You need to get your PlayerScript from the object doing the hitting, so remove your PlayerScript variable and:
function OnTriggerStay(other : collider)
{
var player = other.GetComponent(PlayerScript);
if(player && player.IsAttacking)
{
...
}
}
The variable IsAttacking is not a static variable, it's a public variable. The problem is that the I can't enter the trigger of the enemy even if it is set to Is Trigger. Nor can I harm the enemy. That's what didn't work.
Follow this Question
Related Questions
NullReferenceException Error 2 Answers
4 Error on my script. 1 Answer
Fix this script? 0 Answers
Collision error 1 Answer
Scene Audio Script Error 2 Answers