Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Respawn & Collectibles

Discussion in 'Scripting' started by cisco0911, Jul 13, 2022.

  1. cisco0911

    cisco0911

    Joined:
    Nov 2, 2021
    Posts:
    3
    I have a game that I'm making where you sling the guy and he collects pizzas, but there are things that hurt the player and if the player gets hurt he Respawns to the start point and loses 1 out of three lives.
    The problem lies when the player Respawns after losing a life. He is able to collect the pizzas but it doesn't register in my UI count at the top. However the UI works when the player is full health and hasn't respawned. But once he Respawns the count does not work.

    Once he loses three lives and the retry screen appears I can press replay and it reverts back to the beginning and his collectibles are counted again. But then again the problem lies with the respawn.

    How do I fix this issue? I have done everything I can think of and no matter what the count is lost after respawn. I'm pretty sure it's because I'm destroying the gameObject upon respawn. So the game is somehow reading the old instance of that gameObject and not the new instance. So how do I fix this?

    Thanks.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    Don't destroy the original instance. Just move it back, reset whatever you need to reset and done.

    Otherwise, without seeing any code, we can't really offer much advice about how you have it setup.
     
    cisco0911 likes this.
  3. cisco0911

    cisco0911

    Joined:
    Nov 2, 2021
    Posts:
    3
    Okay so now my player is respawning and collecting pizzas. However the UI count resets to 1 when my player collects a new pizza after respawning. So if I had collected 3 pizzas and died, the player respawns and the UI count stays at 3, but once he collects the next pizza it resets to 1.

    My simple respawn code.

    Code (CSharp):
    1. public Transform respawnPoint;
    2.  
    3.     private void OnCollisionEnter2D(Collision2D collision)
    4.     {
    5.         if (collision.gameObject.CompareTag("Enemy"))
    6.         {
    7.             Instantiate(gameObject, respawnPoint.position, Quaternion.identity);
    8.             Destroy(gameObject);
    9.             AudioManager.instance.Play("Hurt");
    10.         }
    11.     }

    My Pizza UI code

    Code (CSharp):
    1. public class PizzaUI : MonoBehaviour
    2. {
    3.     private TextMeshProUGUI pizzaText;
    4.  
    5.     void Start()
    6.     {
    7.         pizzaText = GetComponent<TextMeshProUGUI>();
    8.     }
    9.  
    10.     public void UpdatePizzaText(PizzaInventory pizzaInventory)
    11.     {
    12.         pizzaText.text = pizzaInventory.NumberOfPizzas.ToString();
    13.     }
    14. }

    and my pizza collectible code

    Code (CSharp):
    1. public class PizzaInventory : MonoBehaviour
    2. {
    3. public int NumberOfPizzas
    4.     {
    5.         get; private set;
    6.     }
    7.  
    8.     public UnityEvent<PizzaInventory> OnPizzaCollected;
    9.  
    10.     public void PizzaCollected()
    11.     {
    12.         NumberOfPizzas++;
    13.         AudioManager.instance.Play("Crunch");
    14.         OnPizzaCollected.Invoke(this);
    15.     }
    16. }

    pizza inventory code

    Code (CSharp):
    1. public class PizzaInventory : MonoBehaviour
    2. {
    3. public int NumberOfPizzas
    4.     {
    5.         get; private set;
    6.     }
    7.  
    8.     public UnityEvent<PizzaInventory> OnPizzaCollected;
    9.  
    10.     public void PizzaCollected()
    11.     {
    12.         NumberOfPizzas++;
    13.         AudioManager.instance.Play("Crunch");
    14.         OnPizzaCollected.Invoke(this);
    15.     }
    16. }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    If your PizzaInventory code is on your player and you destroy your player and create a new player, then yes, your pizza count will be back to 0, thus 1 when you get your first pizza on that new player.

    Think of it as an assembly line. If you're building a cars and your first car comes off the assembly line and you fill the trunk with pizzas but then decide you don't like that car and destroy it, the second car does not have those pizza's in it.

    Granted this is a guess as to what is happening since I don't know how you have things laid out.