• 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 tsphikas · Jul 28, 2020 at 03:44 PM · gameobjectrigidbodycharactercharacter controllercodepage

Intermittent bug occurring when re spawning

Guys I've got an intermittent re-spawning issue. If my character receives to kill shots within milliseconds of each other. Rather than re-spawning like normal on mobile devices it re-spawns my character twice. if anyone could point me in the right direction i'd appreciate it. using System.Collections; using System.Collections.Generic; using UnityEngine;

  public class GameMaster : MonoBehaviour
  {
  
      public static GameMaster gm;
  
      [SerializeField]
      private int maxLives = 3;
      private static int _remainingLives = 3;
      public static int RemainingLives
  
      {
          get { return _remainingLives; }
      }
  
      void Awake()
      {
              if (gm == null)
          {
              gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameMaster>();
          }
      }
  
      public Transform playerPrefab;
      public Transform spawnPoint;
      public float spawnDelay = 2;
      public Transform spawnPrefab;
      public string respawnCountdownSoundName = "RespawnCountdown";
      public string spawnSoundName = "Spawn";
      public string gameOverSoundName = "GameOver";
  
      public CameraShake cameraShake;
  
      [SerializeField]
      private GameObject gameOverUI;
  
      // Cache
      private AudioManager audioManger;
  
      //public HealthBar healthBar;
  
      void Start()
      {
          if(cameraShake == null)
          {
              Debug.LogError("No camera shake");
          }
  
          _remainingLives = maxLives;
  
          // Caching
          audioManger = AudioManager.instance;
          if (audioManger == null)
          {
              Debug.LogError("FREAK OUT! No Audio manager found in scene");
          }
      }
  
      public void EndGame()
      {
  
          audioManger.PlaySound(gameOverSoundName);
  
          Debug.Log("EndGame");
          gameOverUI.SetActive(true);
      }
  
      public IEnumerator _RespawnPlayer ()
      {
          audioManger.PlaySound(respawnCountdownSoundName);
          yield return new WaitForSeconds(spawnDelay);
  
          audioManger.PlaySound(spawnSoundName);
          Instantiate(playerPrefab, spawnPoint.position, spawnPoint.rotation);
          GameObject clone = Instantiate(spawnPrefab.gameObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
          Destroy(clone, 4f);
      }
  
      public static void KillPlayer(Player player)
      {
          
          _remainingLives -= 1;
          if(_remainingLives <= 0)
          {
  
              gm.EndGame();
          }
          else
          {
              gm.StartCoroutine(gm._RespawnPlayer());
          }
          Destroy(player.gameObject);
      }
  
  
      public static void KillEnemy (Enemy enemy)
      {
          gm._killEnemy(enemy);
      }
  
      public void _killEnemy(Enemy _enemy)
      {
          // Let's play some sound
          audioManger.PlaySound(_enemy.deathSoundName);
  
          // Add Particles
          GameObject _clone = Instantiate(_enemy.deathParticles.gameObject, _enemy.transform.position, Quaternion.identity) as GameObject;
          Destroy(_clone, 5f);
  
          // Camera Shake
          //cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
          Destroy(_enemy.gameObject);
      }
  }
  
  
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Player : MonoBehaviour
  {
      [System.Serializable]
  
      public class PlayerStats
      {
          /*public int Health = 100;*/
         
          
          public int maxHealth;
  
          public HealthBar healthBar;
  
          private int _curHealth;
          public int curHealth
          {
              get { return _curHealth; }
              set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
          }
  
          public void Init()
          {
              curHealth = maxHealth;
              healthBar.SetMaxHealth(maxHealth);
          }
      }
  
      public PlayerStats stats = new PlayerStats();
  
      public int fallBoundary = -20;
  
      public string deathSoundName = "DeathVoice";
      public string damageSoundName = "Grunt";
  
      private AudioManager audioManager;
      
      [SerializeField]
      private StatusIndicator statusIndicator;
  
      void Start()
      {
          stats.Init();
  
          if(statusIndicator == null)
          {
              Debug.LogError("Error");
          } 
          else
          {
              
              statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
          }
  
          audioManager = AudioManager.instance;
          if (audioManager == null)
          {
              Debug.LogError("No audio Manager in scene");
          }
  
          
      }
  
      void Update()
      {
          if(transform.position.y <= fallBoundary)
              DamagePlayer(999999);
              
      }
  
      public void DamagePlayer (int damage)
      {
          stats.curHealth -= damage;
          stats.healthBar.SetHealth(stats.curHealth);
  
         
          if (stats.curHealth <= 0)
          {
  
              // Play death sound
              audioManager.PlaySound(deathSoundName);
  
              // Kill player
              GameMaster.KillPlayer(this);
          } else
          {
              // Play damage sound
              audioManager.PlaySound(damageSoundName);
          }
  
          statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
          
      }
      
  
  }
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
0
Best Answer

Answer by Llama_w_2Ls · Jul 28, 2020 at 03:51 PM

Cant you just put in the Awake method: if (gm == null) { gm = this} else {Destroy(gameObject)} This would mean that only one instance of your character exists at one time, meaning that you cant respawn twice.

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
avatar image
0

Answer by unity_ek98vnTRplGj8Q · Jul 28, 2020 at 04:08 PM

Just keep a flag that keeps track of whether or not you have called the "KillPlayer" method yet and return out of the method if you have.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

273 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Destroy declaration but not game object 2 Answers

I can't push a Rigidbody with Character Controller 1 Answer

rigidbody.velocity not working on x axis 2 Answers

Character Controller vs moving platform 0 Answers

Problem with Movement Script 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