- Home /
This Script don't work, Why? / Life players
I have this script:
#pragma strict
var lives = 3;
var livesTexture : Texture2D[];
var livesTextureSize = 30;
function OnGUI ()
{
var livesTextureRect = Rect(10,5,livesTextureSize,livesTextureSize);
for (var i : int = 0; i < lives; i++)
{
GUI.DrawTexture(livesTextureRect,livesTexture[i], ScaleMode.ScaleToFit, true, 0.0f);
livesTextureRect.x += livesTextureSize + 10;
}
}
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Enemy")
{
print("work?");
if (lives-1 >= 0)
livesTexture[lives-1] = null;
WaitForSeconds(2);
Invoke("life", 2);
}
}
function life()
{
Invoke("life2", 2);
lives --;
}
function life2()
{
Invoke("life3", 2);
lives --;
}
function life3()
{
lives --;
print("YOU DEAD");
}
When I enter a trigger for tag "enemy" in general does not detect it. No shows "print (" work ");" What is wrong?
Script added to the First person controller.
The enemy has the tag "Enemy"
Answer by asafsitner · Feb 01, 2014 at 01:48 PM
You're not actually checking the tag, instead you're checking the object's name
if(myTrigger.gameObject.name == "Enemy")
should be
if(myTrigger.gameObject.tag == "Enemy")
And how to modify the script? that only when the player is in the "trigger" fetch life, now is so that when player out from "trigger" further takes life.
This is called when a trigger is exited:
void OnTriggerExit (Collider other) {
//etc
}
If that is what you mean :P
When a player enters the trigger with the tag "Enemy" take life. I want to stop "to take life" when player comes out of the trigger. :P
Your answer
Follow this Question
Related Questions
How do I make an enemy? 1 Answer
Why is my enemy stopping before it reaches the player? 1 Answer
Taking a hit 3 Answers
Spawning enemy mobs .......... Instantiate woe ? 2 Answers
Any one have an AI spaceship script 1 Answer