Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ThomasMarsh · Nov 12, 2012 at 02:47 AM · c#respawnhealth

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);
             }
         }
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ByteSheep · Nov 12, 2012 at 08:27 PM 0
Share

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 ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThomasMarsh · Nov 12, 2012 at 09:16 PM 0
Share

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;"

avatar image Yokimato · Nov 12, 2012 at 09:32 PM 0
Share

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.

avatar image ThomasMarsh · Nov 12, 2012 at 09:59 PM 0
Share

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#.

avatar image Yokimato · Nov 12, 2012 at 10:05 PM 0
Share

"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

avatar image ThomasMarsh · Nov 12, 2012 at 10:15 PM 0
Share

Its like learning from experience. Also, if you suggest learning more about C# what guides would you recommend?

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges