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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Respawn script problem

Discussion in 'Scripting' started by BramVG, Nov 17, 2015.

  1. BramVG

    BramVG

    Joined:
    Nov 15, 2015
    Posts:
    6
    I just started developing with Unity and began learning C# yesterday but I ran into a problem I can't seem to solve.

    The problem is, if I test my game in Unity and I respawn I get my 3 lifes back.
    But when I build the game, and I respawn I only get 2 lifes back.

    Here is my code (not the full code) that I used for the respawning:

    Code (CSharp):
    1.     public int StarterLives; // 3
    2.     public GameObject plr; // My Player
    3.     public float maxVoidDist; // -10
    4.     public Object respawnLevel; // Level01 (My first and only asset)
    5.     public Text LivesHolder; // The text object (UI)
    6.    
    7.     private Vector3 respawnPoint; // This gets updated in Start() and becomes the first position of the player
    8.     private string deadLevel; // This gets updated in Start() and becomes the name of my respawnlevel
    9.     private int lives; // This gets updated in Start() and becomes StarterLives (3)
    10.     private bool delay1 = false;
    11.  
    12.  
    13.     void Update () {
    14.         if ((plr.transform.position.y <= maxVoidDist) && (delay1.Equals(false)))
    15.         {
    16.             delay1 = true;
    17.             if((lives - 1) <= 0)
    18.             {
    19.                 Application.LoadLevel(deadLevel);
    20.                 lives = StarterLives + 1;
    21.             } else
    22.             {
    23.                 plr.transform.position = respawnPoint;
    24.             }
    25.  
    26.             lives = lives - 1;
    27.             updateLives();
    28.             delay1 = false;
    29.         }
    30.     }
    31.  
    32.     void updateLives()
    33.     {
    34.         LivesHolder.text = "Lives: " + lives;
    35.     }
    Full code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class GameController : MonoBehaviour {
    6.  
    7.     public int StarterLives;
    8.     public GameObject plr;
    9.     public float maxVoidDist;
    10.     public Object respawnLevel;
    11.     public Text LivesHolder;
    12.    
    13.     private Vector3 respawnPoint;
    14.     private string deadLevel;
    15.     private int lives;
    16.     private bool delay1 = false;
    17.  
    18.     void Awake()
    19.     {
    20.         QualitySettings.vSyncCount = 1;
    21.     }
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.         respawnPoint = plr.transform.position;
    26.         deadLevel = respawnLevel.name;
    27.         lives = StarterLives;
    28.         updateLives();
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.         if ((plr.transform.position.y <= maxVoidDist) && (delay1.Equals(false)))
    34.         {
    35.             delay1 = true;
    36.             if((lives - 1) <= 0)
    37.             {
    38.                 Application.LoadLevel(deadLevel);
    39.                 lives = StarterLives + 1;
    40.             } else
    41.             {
    42.                 plr.transform.position = respawnPoint;
    43.             }
    44.  
    45.             lives = lives - 1;
    46.             updateLives();
    47.             delay1 = false;
    48.         }
    49.     }
    50.  
    51.     void updateLives()
    52.     {
    53.         LivesHolder.text = "Lives: " + lives;
    54.     }
    55. }
    56.  

    Any help would be greatly appreciated, thank you.
     
  2. S_Rank_Crazy

    S_Rank_Crazy

    Joined:
    Feb 3, 2014
    Posts:
    26
    Why not just set lives to StarterLives when you reset the position to the respawnPoint. If that's what you want to happen, why not just make it happen?
     
  3. BramVG

    BramVG

    Joined:
    Nov 15, 2015
    Posts:
    6
    I want to create more levels and the respawn should occur after you die.
    But once all your lives are up, I want to completely restart the game.
     
  4. BramVG

    BramVG

    Joined:
    Nov 15, 2015
    Posts:
    6
    I have been trying to fix it, and it seems like the problem is that some objects haven't been loaded once the game starts, is there any way to wait for an object to load?
     
  5. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
    why not do something like

    start()
    {
    //lives is set to starter lives (which should be 3 in your inspector) make sure it is
    lives = starterLives;
    //now our lives is set to 3 (i assume this is what your start method looks like)
    }
    (im assuming this gets called when your player dies)
    if(lives - 1 <=0)
    {
    //do stuff to restart your level
    lives = starterLives; //resets your lives if the loaded level doesn't already
    }
    else //if your lives didn't drop to 0
    {
    //respawn your player
    lives = lives -1; //or lives -= 1;
    }

    try this. im not really sure either why that's in the update method either but this may work..
     
  6. BramVG

    BramVG

    Joined:
    Nov 15, 2015
    Posts:
    6
    That's what I did, or is there a difference?
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    my advice would be to break things down to the relevant parts rather than trying to handle everything in one place.

    Have a lives script which handles the lives count and dying. It just need to keep track of the numbers and supports a "harm player" method of some sort, when the player loses a life throw a "respawn player event", when they lose all their lives throw a "player dead event".

    Have a respawn script which just keeps a track of the respawn point, and has a listener for "respawn player events".

    Have a collider which harms the player across the bottom of the level, so when it collides with the player (or more accurately the player collides with it) it can call the "harm player" function.

    Have a gamecontroller which listens for "player dead events" and reloads the level or whatever.
     
  8. BramVG

    BramVG

    Joined:
    Nov 15, 2015
    Posts:
    6
    Ok thanks, I already abandoned the project now.
    It was my first and it was a pretty good learning opportunity.