- Home /
Respawning without creating Clones
I have been trying for a while to get the player to respawn without the player having to restart the game. However, no matter what variation I try, I cannot seem to get 1 player to be in the game. Currently, it creates a loop making tons of players. How could I fix my current script or redesign it to make it do what it is designed to?
The variable play is set to a prefab of the player Also, bonus points if you can find out how to fix my regen script to regenerate 10 health every 5 seconds.
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public float MaxHealth=100;
public float CurrentHealth;
public bool Invincible;
public bool Dead;
public bool run = false;
public bool regen = true;
public Transform play;
public Transform fplay;
// Use this for initialization
void Awake () {
//MAKE THE CURRENT HEALTH THE MAX HEALTH AT START
CurrentHealth=MaxHealth;
}
//regen doesnt work yet
IEnumerator Start() {
if(regen=true && CurrentHealth>0){
yield return new WaitForSeconds(5);
run = true;
yield return 0;
run = false;
}
}
// Update is called once per frame
void Update () {
//IF INVINCIBLE, HE CANNOT DIE..
if(Invincible){
CurrentHealth=MaxHealth;
}
else{
if(CurrentHealth<=0){
CurrentHealth=0;
Dead=true;
}
//MAX HEALTH
if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
if(run){
CurrentHealth=(CurrentHealth+=10);
}
//WHEN DEATH IS UPON HIM
if(Dead){
CurrentHealth=MaxHealth;
var cplay = GameObject.Find ("Player");
var obj = GameObject.Find ("Respawn");
Instantiate(play, obj.transform.position, Quaternion.identity);
Dead=false;
Destroy (gameObject);
}
}
}
}
I'm not great at C# or using classes, but I thought I'd just point out a couple small typos I noticed:
if(regen=true && CurrentHealth>0){In this line there need to be two equals signs between 'regen' and 'true' since it is a comparison.
CurrentHealth=(CurrentHealth+=10);Here it is unnecessary to write it like that, ins$$anonymous$$d use:
CurrentHealth+=10;I know this probably won't solve the original problem - but maybe someone with more brains than me will be able to figure it out ;)
Answer by markpdolby · Nov 12, 2012 at 08:22 PM
Instead of creating a new player and destroying the old one, why not just reuse the current gameobject of the player? So in the script instead of this:
CurrentHealth=MaxHealth;
var cplay = GameObject.Find ("Player");
var obj = GameObject.Find ("Respawn");
Instantiate(play, obj.transform.position, Quaternion.identity);
Dead=false;
Destroy (gameObject);
do something like this:
CurrentHealth=MaxHealth;
var obj = GameObject.Find ("Respawn");
transform.position = obj.position;
Dead=false;
Also the reason why you are getting an infinite amount of players spawing is because
var cplay = GameObject.Find ("Player");
is finding the current player object and not the prefab, if you wish to use the prefab then you need to assign it in the inspector.
for the regen do something like this:
void Regen()
{
if(CurrentHealth + 10 < MaxHealth)
{
CurrentHealth += 10;
}else if(CurrentHealth < MaxHealth){
CurrentHealth = MaxHealth;
}
}
void Start()
{
InvokeRepeating("Regen", 5, 5); //Will call the method every 5 seconds
}
Thank you for the suggestion to just use the current player, however, when I put in the changes to the code it gives me the error that "Type `UnityEngine.GameObject' does not contain a definition for `position' and no extension method `position' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)" on the line "transform.position = obj.position;"
It's just a typo. To fix, simply make the right hand side of the assignment "obj.transform.position". However, a more concerning issue is that you might want to brush up on your coding skills since I don't think markpdolby's response was to be taken verbatim but rather as a guideline. Copy and paste won't complete a game for you.
I am doing this as a project for school, this is not a full length game. I am mostly just trying to get a working game up. However, it builds fine now but when the enemy AI's attack me, I do not take damage, and the damage script for AI is fine. Once I finish this project I will try to fully learn C#.
"Once I finish this project I will try to fully learn C#."
That's a silly statement :P That's like saying I'm going to rebuild my car engine first and then learn how a car engine works.
Good luck
Its like learning from experience. Also, if you suggest learning more about C# what guides would you recommend?
Answer by shaystibelman · Nov 13, 2012 at 04:46 PM
Umm... really REALLY stupid question: Why not just move the player to the spawn position instead of destroying and recreating game objects? Just reset his Vector3 and all the other variables you want to reset. That's what I'd do.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083725im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how to display the damage value from a mob 1 Answer
My script to kill the player and respawn, does nothing? 1 Answer
Distribute terrain in zones 3 Answers
C# Respawn Help 1 Answer