Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TCROC · Jun 24, 2016 at 07:22 PM · invokeinvokerepeating

Invoke () Working like InvokeRepeating ()

I am calling this invoke function after my player dies. I want him to respawn with a delay. It does get called after a five second delay, but then it gets called for five seconds as well and my player is stuck in his position for five seconds. Any help is greatly appreciated. :)

 using UnityEngine;
 using System.Collections;
 
 public class Player_Death : MonoBehaviour {
 
     private GameObject spawnPoint;
     public Vector3 startPos;
     public Transform armature;
     public GameObject lowestLevel;
     public bool dead = false;
 
     private bool callRespawn;
 
     public GameObject center;
 
     // Use this for initialization
     void Start () {
         spawnPoint = GameObject.Find ("SpawnPoint");
         startPos = spawnPoint.transform.position;
         startPos.y += 1f;
         if (armature != null) {
             foreach (Transform child in armature) {
                 child.position = startPos;
             }
         }
 
         callRespawn = true;
     }
 
     // Update is called once per frame
     void Update () {
         if (center.transform.position.y <= lowestLevel.transform.position.y) {
             dead = true;
         }
         if (dead == true && armature != null) {
             Invoke ("Respawn", 5f);
             
         } else if (dead == true && armature == null) {
             transform.position = startPos;
             transform.root.GetComponent <Player_Health> ().health = 100f;
             dead = false;
         }
         print (dead);
     }
 
     private void Respawn () {
         print ("here " + 1 + Time.time);
     
         
         foreach (Transform child in armature) {
             child.GetComponent<Rigidbody> ().velocity = Vector3.zero;
             child.position = startPos;
             child.GetComponent<Rigidbody> ().isKinematic = false;
         }
         transform.root.GetComponent <Player_Health> ().health = 100f;
         dead = false;
 
     }
 }
Comment
Add comment
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

2 Replies

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

Answer by tanoshimi · Jun 24, 2016 at 07:28 PM

That's because, as your own comment states // Update is called once per frame. So, after your player dies, you're calling a new Invoke() every frame until they respawn. It's only after 5 seconds, when the Respawn function first runs, that you set dead = false and stop calling Invoke, but by then you've got 5 seconds worth of queued calls to get through - which is when your character freezes.

The solution would be to set

      if (dead == true && armature != null) {
          Invoke ("Respawn", 5f);
          dead = false;   
      }

Or perhaps add a three-way state of "dead", "alive", "waitingToRespawn" or something like that.

Comment
Add comment · Show 2 · 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 Raresh · Jun 24, 2016 at 07:31 PM 0
Share

That comment was generated automatically :D

avatar image TCROC · Jun 26, 2016 at 04:12 AM 0
Share

Thanks for that quick and simple explanation!

avatar image
1

Answer by Dave-Carlile · Jun 24, 2016 at 07:23 PM

 if (dead == true && armature != null) {
          Invoke ("Respawn", 5f);

That code is getting executed every frame, so you're calling Invoke over and over while those conditions are true. You need something to indicate that you've already invoked the respawn and add that to your if condition to make sure it doesn't keep calling Invoke.

Another way to deal with this sort of things is to set up a simple state machine using an enum and switch statement.

 enum State { Running, Dead, WaitForRespawn, Respawn }

 State state = State.Running;

 ....

 void Update()
 {
    switch (state)
    {
      case State.Running:
        DoRunningStuff();
        if (center.transform.position.y <= lowestLevel.transform.position.y)
        {
          state = State.Dead;
        }
        break;

       case State.Dead:
         Invoke("Respawn", 5.0f);
         state = State.WaitForRespawn;
         break;

       case State.WaitForRespawn:
         // don't really do anything, or show a countdown, whatever
         break;

       case State.Respawn:
         // do respawn stuff

         // change state back to running
         state = State.Running;
         break;
      
    }
 }

 void Respawn()
 {
   ...
   state = State.Respawn;
 }


That doesn't cover everything you have in your example but it gets the point across. This sort of thing can often be more clear than checking a bunch of different variables to decide what to do.

Comment
Add comment · Show 1 · 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 TCROC · Jun 26, 2016 at 04:14 AM 0
Share

Thanks for the examples of enum and switch statements. I am going to start using some of those more often. :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to change the bullet fire rate in C#? 1 Answer

Simple timer using InvokeRepeating 2 Answers

why won't an invokerepeating cancel? 1 Answer

How to subtract live when a game object destroy itself? 0 Answers

(SOLVED) Destroy(gameObject) doesn't destroy the gameObject, only disables behaviours. 0 Answers


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