- Home /
OnTriggerEnter can't detect my Transform tag
I'm making a simple zombie survival game. I've a problem with this code, this script detect only one trigger, but the other, no.
#pragma strict
var health = 100;
var attack = 10;
var delay = 5;
var scream : AudioClip;
var player : Collider;
function OnTriggerEnter () {
if (player.gameObject.tag == "ZombieFlame") {
gameObject.Find("Flame").SendMessage("OnTriggerEnter");
}
if (player.gameObject.tag == "Zombie") {
Attack ();
}
if (health == 0) {
Debug.Log("Die!");
Lose ();
}
}
function Attack () {
health -= attack;
Debug.Log("Under attack!");
audio.PlayOneShot(scream);
yield WaitForSeconds(delay);
Loop ();
}
function Loop () {
OnTriggerEnter ();
}
function Lose () {
this.active = false;
}
My script detects the "ZombieFlame" but not the "Zombie", that Transform have already the tag, I don't know what is going on. It's checked like Trigger, too.
Any help will be gladly received, thank you very much!
you have to Find the Zombie Game Object......
player.gameobject = GameObject.find(" your object name");
Answer by admanic · Apr 19, 2015 at 02:06 PM
The OnTriggerEnter function should take a collider:
function OnTriggerEnter (other : Collider) {
//CODE
}
Then where //CODE is you can do:
if(other.gameObject.tag == "Flame")
{
// DO WHATEVER
}
The collider that is passed in through OnTriggerEnter is the collider that has hit your trigger. So this is the object you need to check to see if its your flame or zombie.
Unless you're not using Monobehaviour with this script?
I already tried with that... I receive an error:
The best overload from the method Health.OnTriggerEnter(UnityEngine.Collider) is not compatible with the argument list '()'.
Your answer
Follow this Question
Related Questions
How to make OnTriggerEnter and OnTriggerExit functions more than once? 1 Answer
Get a UserTrigger in Javascript - Eg for SmoothMoves 1 Answer
variable = true from another script 2 Answers
Calling JS Static Function from C# Script 1 Answer
How do I call a funtion without refrencing the script? 1 Answer