- Home /
Help - Player Respawn Help
To all the people of the Unity world wide website, if you could give me a moment of your time and help me figure out why my script isn't working. You see I've downloaded a simple health system that I obtain on the Unity Asset store (Grid Digital - Simple Health System) to create a health system, and have being trying to create a scrip that will respawn the player once they lost all there health.
However no matter what I try it doesn't work, you see I've coded a respawn script, and it works perfectly fine. var startPosition : Transform;
function OnTriggerEnter(theCollider : Collider)
{
theCollider.transform.position = startPosition.position;
}
As you can see the script works, however when I tried to create my own script that states that the player will respawn once he/she loses all his health. var OneHeart : Texture2D; //These variables contain the textures used for the gui. var TwoHearts : Texture2D; var ThreeHearts : Texture2D; var FourHearts : Texture2D; var NoHearts : Texture2D;
static var Hearts : int = 4; //This is the amount of hearts the character has.
var startPosition : Transform;
function OnTriggerEnter(theCollider : Collider)
{
if(Hearts < 0)
{
Hearts = 0;
}
else
{
theCollider.transform.position = startPosition.position;
guiTexture.texture = FourHearts;
}
}
For my life I can't see what is wrong, the script should work fine, but it doesn't. Additionally the health script form the Simple Health System is as followed...
var OneHeart : Texture2D; //These variables contain the textures used for the gui.
var TwoHearts : Texture2D;
var ThreeHearts : Texture2D;
var FourHearts : Texture2D;
var NoHearts : Texture2D;
static var Hearts : int = 4; //This is the amount of hearts the character has.
function Update (){
if(Hearts == 4){ //These if statements change the Gui Texture based on the static var Hearts.
guiTexture.texture=FourHearts;
}
if(Hearts == 3){
guiTexture.texture=ThreeHearts;
}
if(Hearts == 2){
guiTexture.texture=TwoHearts;
}
if(Hearts == 1){
guiTexture.texture=OneHeart;
}
if(Hearts == 0){
guiTexture.texture=NoHearts;
}
if(Hearts > 4){ //These two if statements keep the static var Hearts within the 0-4 range.
Hearts = 4;
}
if(Hearts < 0){
Hearts = 0;
}
}
I was wondering if anyone could just look over my script and see why it is not working, any help would be great. Go dtí an chéad uair eile, slán leat
I'm confused. For your respawn, what is setting off OnTriggerEnter(). You'r respawn will not work unless something is triggering this method.
As a double check, when you bring your player back to life, are you restting his health as well? If not, he may come back to to live to only immediately die again.
I didn't know that, I though that if the player enter the trigger then the respawn script would work. But for some reason it doesn't, however do you now of anyway to have the player respawn when he/she loses all there health.
Answer by rednax20 · Mar 08, 2013 at 02:27 AM
I don't know what your doing exactly, but for player respawns you could always reload the level that you are on. Although I don't know what's wrong with your code
if(Hearts = 0){
Application.LoadLevel( "The level you are playing" );
}
This won't work if you just want them to respawn, and you want the things that they have interacted with to remain.
I don't know much about transforms, but on a wild guess, is line 13 supposed to say
theCollider.transform.position = startPosition.transform. position;
Your right, I didn't notice the mistake on line 13, thanks for pointing it out. I'll give it a check and see if changing that works. Additionally thanks for the help, I'll give the script you wrote a try, if I can't respawn the player, reloading the level will have to do.
Answer by TargonStudios · Mar 08, 2013 at 03:45 AM
You could try Instantiating a new Character Controller with all the scripts attached and deleting the old one when the health equals 0.
Your answer
