Search Unity

Spawn player to try again immediately ends game c# ??

Discussion in 'Scripting' started by caseyboundless, Mar 24, 2013.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I resolved the problem.




    When my players health goes to 0 it subtracts a life. This works and puts the player back at the start but then immediately ends the game. Is there a better solution?



    https://dl.dropbox.com/u/56545886/StarHawk/StarHawk.html WEBPLAYER







    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class scrGameController : MonoBehaviour {
    5.    
    6.     //public Camera cameraFollowWithZoom;                           // Camera 1 rear view  
    7.    
    8.     public Transform transformPlayer;                               // Character transform
    9.     private CharacterController characterControllerPlayer;      // Character controller
    10.    
    11.     public Transform deathZoneTransform;                            // Death zone plane, if player falls below it, he dies
    12.    
    13.     public float speed;
    14.    
    15.     private TextMesh healthText;  
    16.     private TextMesh percent;
    17.     public float playerHealth;
    18.     public int minPlayerHealth = 0;
    19.     public int maxPlayerHealth = 100;
    20.     public bool percent25 = false;
    21.    
    22.     private TextMesh textScore;
    23.     public int score;
    24.     public int points = 0;
    25.     private TextMesh lives;
    26.     public int playerLife;
    27.  
    28.     public GameObject player;
    29.    
    30.     public Transform spawnLocationPlayer;
    31.    
    32.    
    33.    
    34.     // Use this for initialization
    35.    
    36.    
    37.    
    38.    
    39.    
    40.     void Awake()
    41.     {
    42.        
    43.        
    44.         if(Application.loadedLevelName != "Stage11")
    45.         {
    46.            
    47.         points = PlayerPrefs.GetInt("currentScore");
    48.         playerLife = PlayerPrefs.GetInt("currentLives");
    49.            
    50.            
    51.         }
    52.  
    53.        
    54.        
    55.        
    56.        
    57.     }
    58.    
    59.    
    60.    
    61.    
    62.    
    63.    
    64.    
    65.    
    66.    
    67.    
    68.    
    69.     void Start ()
    70.     {
    71.         playerHealth = maxPlayerHealth;
    72.         healthText = GameObject.Find("3DTextHealth").GetComponent<TextMesh>();
    73.         textScore = GameObject.Find("3DTextScore").GetComponent<TextMesh>();
    74.        
    75.           lives = GameObject.Find("3DTextLives").GetComponent<TextMesh>();
    76.        
    77.         healthText.text = playerHealth.ToString(); 
    78.         textScore.text = points.ToString();
    79.         playerLife = 2;
    80.         lives.text = playerLife.ToString();
    81.     }
    82.    
    83.     void Update()
    84.     {
    85.        
    86.        
    87.         //InvokeRepeating("UpdatePoints" , 10,5, 0.1f);
    88.        
    89.         if(playerHealth <= minPlayerHealth)
    90.         {
    91.             //playerHealth = minPlayerHealth;  
    92.             transform.position = new Vector3(transform.position.x,-1.3f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    93.             SubtractLife();
    94.             //StartCoroutine(Death());
    95.        
    96.         }
    97.        
    98.        
    99.         if(playerHealth <= 25)
    100.         {
    101.           healthText.renderer.material.color = Color.red;
    102.              
    103.              
    104.         }
    105.        
    106.        
    107.        
    108.        
    109.        
    110.        
    111.             healthText.text = playerHealth.ToString();
    112.        
    113.          textScore.text = points.ToString();
    114.        
    115.         lives.text = playerLife.ToString();
    116.        
    117.         Debug.Log("Player health: " + playerHealth);
    118.        
    119.      
    120.        
    121.         float moveAmout = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime; //moves player left and right
    122.         transform.Translate(Vector3.right * moveAmout); // moves player left and right
    123.    
    124.         float moveAmout2 = Input.GetAxisRaw("Vertical") * speed * Time.deltaTime;
    125.    
    126.         transform.Translate(Vector3.up * moveAmout2);
    127.    
    128.         if(transform.position.x <= -6.1f) //// if player leaves the screen will pop up on oppisite side
    129.         {
    130.             transform.position = new Vector3(5.8f, transform.position.y, transform.position.z); // if player leaves the screen will pop up on oppisite side
    131.         }
    132.    
    133.    
    134.         if(transform.position.x >= 6.1f) //// if player leaves the screen will pop up on oppisite side
    135.         {
    136.             transform.position = new Vector3(-5.8f, transform.position.y, transform.position.z); // if player leaves the screen will pop up on oppisite side
    137.         }
    138.    
    139.    
    140.    
    141.      if(transform.position.y <= -4.573847f) //// if player leaves the screen will pop up on oppisite side
    142.      
    143.         {
    144.            
    145.             transform.position = new Vector3( transform.position.x, 4.573845f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    146.            
    147.        
    148.         }
    149.    
    150.    
    151.     if(transform.position.y >= 4.573847f) //// if player leaves the screen will pop up on oppisite side
    152.         {
    153.             transform.position = new Vector3(transform.position.x,-4.573845f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    154.         }
    155.     }
    156.  
    157.  
    158.        
    159.    
    160.    
    161.  
    162.    
    163.    
    164.    
    165.     public void UpdatePoints()
    166. {
    167.     points += 1;                                                                //add x amount points
    168.     textScore.text = "" + "    " + points;                                  //update 3D text with the points
    169. }
    170.    
    171.    
    172.    
    173.    
    174.    
    175.    
    176.    
    177.    
    178.    
    179.    
    180.    
    181.    
    182.    
    183.    
    184.    
    185.     public void addScoreKill ()
    186.     {
    187.         points = points + 100;
    188.    
    189.        
    190.     }
    191.    
    192.    
    193.    
    194.    
    195.    
    196.    
    197.    
    198.    
    199.    
    200.    
    201.    
    202.    
    203.    
    204.    
    205.    
    206.    
    207.    
    208.     IEnumerator Death()
    209. {
    210.      
    211.  
    212.         //vp_FPSPlayerScript.enabled = false;
    213.         float timeToWait = 2.4f;
    214.  
    215.      yield return new WaitForSeconds(timeToWait);
    216.         SubtractLife();
    217.         Application.LoadLevel(Application.loadedLevel);
    218.  
    219.     }
    220.  
    221.  
    222.    
    223.    
    224.     void OnTriggerEnter(Collider other)
    225.     {
    226.         if(other.gameObject.tag ==("Enemy"))
    227.         {
    228.           playerHealth = playerHealth - 25.0f;
    229.         }
    230.     }
    231.      
    232.        
    233.        
    234.    
    235.    
    236.    
    237.        
    238.        
    239.        
    240.        
    241.        
    242.        
    243.  
    244.    
    245.     public void leftButton ()
    246.     {
    247.      
    248.          transform.Translate(Vector3.right * -1 * Time.deltaTime); // moves player left and right
    249.    
    250.     }
    251.    
    252.    
    253.         public void rightButton ()
    254.     {
    255.      
    256.          transform.Translate(Vector3.right * 1 * Time.deltaTime); // moves player left and right
    257.    
    258.     }
    259.  
    260.    
    261.    
    262.    
    263.    
    264.     public void upButton ()
    265.     {
    266.      
    267.          transform.Translate(Vector3.up * 1 * Time.deltaTime); // moves player left and right
    268.    
    269.     }
    270.    
    271.    
    272.    
    273.     public void downButton ()
    274.     {
    275.      
    276.          transform.Translate(Vector3.up * -1 * Time.deltaTime); // moves player left and right
    277.    
    278.     }
    279.    
    280.    
    281.    
    282.    
    283.    
    284.     public void CheckDeathZone()
    285.     {
    286.         if(transformPlayer.position.y < deathZoneTransform.position.y)
    287.         {
    288.             transformPlayer.position = new Vector3(0, 1, 0);
    289.         }
    290.    
    291.    
    292.     }
    293.    
    294.        
    295.    
    296.  
    297.  
    298.     void GameOver()
    299. {
    300.     if(points > PlayerPrefs.GetInt("highScore"))                                //if current points > stored survivalScore (highscore)          
    301.     {
    302.         PlayerPrefs.SetInt("highScore", points);                                //save the new score
    303.         PlayerPrefs.Save();
    304.         //audio.PlayOneShot(thirdSound);                                            //play the sound
    305.     }
    306.  
    307.    ToMenu();                                                                    //load main menu
    308. }
    309.  
    310.  
    311.  
    312.   void NextLevel()
    313. {
    314.     PlayerPrefs.SetInt("currentScore" , points);                                //save the current score
    315.     PlayerPrefs.Save();
    316.    
    317.     if(Application.loadedLevelName != "Level4")//Level8                         //if we haven't reached last level
    318.     {
    319.         Application.LoadLevel(Application.loadedLevel + 1);                     //load next level
    320.     }
    321.     else
    322.     {
    323.         if(points > PlayerPrefs.GetInt("highScore"))                            //if current points > stored survivalScore (highscore)          
    324.         {
    325.             PlayerPrefs.SetInt("highScore", points);                            //save the new score only if it's greater than the current highscore
    326.             PlayerPrefs.Save();
    327.         }
    328.        
    329.     ToMenu();                                                                   //else load main menu
    330.     }                          
    331. }
    332.  
    333. void ToMenu()
    334. {
    335.     Application.LoadLevel("MenuScene");                                         //load main menu
    336. }
    337.  
    338.  
    339.  
    340.  
    341. void SubtractLife()
    342. {
    343.     playerLife -= 1;                                                                                //subtract x amount from playerLife
    344.    // Instantiate(player,spawnLocationPlayer.transform.position,spawnLocationPlayer.transform.rotation);
    345.    
    346.         //transform.position = new Vector3(transform.position.x,-1.3f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    347.         maxPlayerHealth = 100;
    348.         if (playerLife > 0 )                                                                            //if player life is greater than 0
    349.     {
    350.         Debug.Log("MainGame: Player lives left :" + playerLife);                                    //spit out the debug log
    351.         PlayerPrefs.SetInt("currentLives", playerLife);                                             //save playerLife temporarily (for next level)
    352.        
    353.    
    354.         // Instantiate(player,transformPlayer.transform.position,transform.rotation);
    355.         }
    356.    
    357.     if(playerLife <= 0)                                                                             //if we have 0 lives
    358.     {
    359.         Debug.Log("MainGame: Player has no more lives left, returning to main menu");               //spit out the debug log
    360.         GameOver();                                                                                 //enter GameOver function
    361.     }
    362. }
    363.  
    364.  
    365.  
    366.  
    367.  
    368. }      
    369.        
    370.        
     
    Last edited: Mar 25, 2013
  2. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Please remove the unneeded blank lines. This looks very messy.

    Code (csharp):
    1.  
    2. #region Imports
    3.  
    4. using System.Collections;
    5. using UnityEngine;
    6.  
    7. #endregion
    8.  
    9. public class scrGameController : MonoBehaviour
    10. {
    11.     //public Camera cameraFollowWithZoom;                           // Camera 1 rear view  
    12.  
    13.     #region Public Fields
    14.  
    15.     public Transform deathZoneTransform; // Death zone plane, if player falls below it, he dies
    16.     public int maxPlayerHealth = 100;
    17.     public int minPlayerHealth = 0;
    18.     public bool percent25 = false;
    19.     public GameObject player;
    20.     public float playerHealth;
    21.     public int playerLife;
    22.     public int points = 0;
    23.     public int score;
    24.     public Transform spawnLocationPlayer;
    25.     public float speed;
    26.     public Transform transformPlayer; // Character transform
    27.  
    28.     #endregion
    29.  
    30.     #region Private Fields
    31.  
    32.     private CharacterController characterControllerPlayer; // Character controller
    33.     private TextMesh healthText;
    34.     private TextMesh lives;
    35.     private TextMesh percent;
    36.     private TextMesh textScore;
    37.  
    38.     #endregion
    39.  
    40.     #region Public Methods
    41.  
    42.     public void CheckDeathZone()
    43.     {
    44.         if (transformPlayer.position.y < deathZoneTransform.position.y)
    45.         {
    46.             transformPlayer.position = new Vector3(0, 1, 0);
    47.         }
    48.     }
    49.  
    50.  
    51.     // Use this for initialization
    52.  
    53.  
    54.     public void UpdatePoints()
    55.     {
    56.         points += 1; //add x amount points
    57.  
    58.         textScore.text = "" + "    " + points; //update 3D text with the points
    59.     }
    60.  
    61.  
    62.     public void addScoreKill()
    63.     {
    64.         points = points + 100;
    65.     }
    66.  
    67.     public void downButton()
    68.     {
    69.         transform.Translate(Vector3.up*-1*Time.deltaTime); // moves player left and right
    70.     }
    71.  
    72.  
    73.     public void leftButton()
    74.     {
    75.         transform.Translate(Vector3.right*-1*Time.deltaTime); // moves player left and right
    76.     }
    77.  
    78.  
    79.     public void rightButton()
    80.     {
    81.         transform.Translate(Vector3.right*1*Time.deltaTime); // moves player left and right
    82.     }
    83.  
    84.     public void upButton()
    85.     {
    86.         transform.Translate(Vector3.up*1*Time.deltaTime); // moves player left and right
    87.     }
    88.  
    89.     #endregion
    90.  
    91.     #region Private Methods
    92.  
    93.     private void Awake()
    94.     {
    95.         if (Application.loadedLevelName != "Stage11")
    96.         {
    97.             points = PlayerPrefs.GetInt("currentScore");
    98.  
    99.             playerLife = PlayerPrefs.GetInt("currentLives");
    100.         }
    101.     }
    102.  
    103.     private IEnumerator Death()
    104.     {
    105.         //vp_FPSPlayerScript.enabled = false;
    106.  
    107.         float timeToWait = 2.4f;
    108.  
    109.  
    110.         yield return new WaitForSeconds(timeToWait);
    111.  
    112.         SubtractLife();
    113.  
    114.         Application.LoadLevel(Application.loadedLevel);
    115.     }
    116.  
    117.     private void GameOver()
    118.     {
    119.         if (points > PlayerPrefs.GetInt("highScore")) //if current points > stored survivalScore (highscore)          
    120.         {
    121.             PlayerPrefs.SetInt("highScore", points); //save the new score
    122.  
    123.             PlayerPrefs.Save();
    124.  
    125.             //audio.PlayOneShot(thirdSound);                                            //play the sound
    126.         }
    127.  
    128.         ToMenu(); //load main menu
    129.     }
    130.  
    131.     private void NextLevel()
    132.     {
    133.         PlayerPrefs.SetInt("currentScore", points); //save the current score
    134.         PlayerPrefs.Save();
    135.  
    136.         if (Application.loadedLevelName != "Level4") //Level8                         //if we haven't reached last level
    137.         {
    138.             Application.LoadLevel(Application.loadedLevel + 1); //load next level
    139.         }
    140.         else
    141.         {
    142.             if (points > PlayerPrefs.GetInt("highScore")) //if current points > stored survivalScore (highscore)          
    143.             {
    144.                 PlayerPrefs.SetInt("highScore", points); //save the new score only if it's greater than the current highscore
    145.                 PlayerPrefs.Save();
    146.             }
    147.             ToMenu(); //else load main menu
    148.         }
    149.     }
    150.  
    151.     private void OnTriggerEnter(Collider other)
    152.     {
    153.         if (other.gameObject.tag == ("Enemy"))
    154.         {
    155.             playerHealth = playerHealth - 25.0f;
    156.         }
    157.     }
    158.  
    159.     private void Start()
    160.     {
    161.         playerHealth = maxPlayerHealth;
    162.  
    163.         healthText = GameObject.Find("3DTextHealth").GetComponent<TextMesh>();
    164.  
    165.         textScore = GameObject.Find("3DTextScore").GetComponent<TextMesh>();
    166.  
    167.  
    168.         lives = GameObject.Find("3DTextLives").GetComponent<TextMesh>();
    169.  
    170.  
    171.         healthText.text = playerHealth.ToString();
    172.  
    173.         textScore.text = points.ToString();
    174.  
    175.         playerLife = 2;
    176.  
    177.         lives.text = playerLife.ToString();
    178.     }
    179.  
    180.     private void SubtractLife()
    181.     {
    182.         playerLife -= 1; //subtract x amount from playerLife
    183.         // Instantiate(player,spawnLocationPlayer.transform.position,spawnLocationPlayer.transform.rotation);
    184.         //transform.position = new Vector3(transform.position.x,-1.3f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    185.  
    186.         maxPlayerHealth = 100;
    187.  
    188.         if (playerLife > 0) //if player life is greater than 0
    189.         {
    190.             Debug.Log("MainGame: Player lives left :" + playerLife); //spit out the debug log
    191.             PlayerPrefs.SetInt("currentLives", playerLife); //save playerLife temporarily (for next level)
    192.             // Instantiate(player,transformPlayer.transform.position,transform.rotation);
    193.         }
    194.         if (playerLife <= 0) //if we have 0 lives
    195.         {
    196.             Debug.Log("MainGame: Player has no more lives left, returning to main menu"); //spit out the debug log
    197.             GameOver(); //enter GameOver function
    198.         }
    199.     }
    200.  
    201.     private void ToMenu()
    202.     {
    203.         Application.LoadLevel("MenuScene"); //load main menu
    204.     }
    205.  
    206.     private void Update()
    207.     {
    208.         //InvokeRepeating("UpdatePoints" , 10,5, 0.1f);
    209.  
    210.  
    211.         if (playerHealth <= minPlayerHealth)
    212.         {
    213.             //playerHealth = minPlayerHealth;  
    214.  
    215.             transform.position = new Vector3(transform.position.x, -1.3f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    216.  
    217.             SubtractLife();
    218.  
    219.             //StartCoroutine(Death());
    220.         }
    221.  
    222.  
    223.         if (playerHealth <= 25)
    224.         {
    225.             healthText.renderer.material.color = Color.red;
    226.         }
    227.  
    228.  
    229.         healthText.text = playerHealth.ToString();
    230.  
    231.  
    232.         textScore.text = points.ToString();
    233.  
    234.  
    235.         lives.text = playerLife.ToString();
    236.  
    237.  
    238.         Debug.Log("Player health: " + playerHealth);
    239.  
    240.  
    241.         float moveAmout = Input.GetAxisRaw("Horizontal")*speed*Time.deltaTime; //moves player left and right
    242.  
    243.         transform.Translate(Vector3.right*moveAmout); // moves player left and right
    244.  
    245.  
    246.         float moveAmout2 = Input.GetAxisRaw("Vertical")*speed*Time.deltaTime;
    247.  
    248.  
    249.         transform.Translate(Vector3.up*moveAmout2);
    250.  
    251.  
    252.         if (transform.position.x <= -6.1f) //// if player leaves the screen will pop up on oppisite side
    253.         {
    254.             transform.position = new Vector3(5.8f, transform.position.y, transform.position.z); // if player leaves the screen will pop up on oppisite side
    255.         }
    256.  
    257.  
    258.         if (transform.position.x >= 6.1f) //// if player leaves the screen will pop up on oppisite side
    259.         {
    260.             transform.position = new Vector3(-5.8f, transform.position.y, transform.position.z); // if player leaves the screen will pop up on oppisite side
    261.         }
    262.  
    263.  
    264.         if (transform.position.y <= -4.573847f) //// if player leaves the screen will pop up on oppisite side
    265.         {
    266.             transform.position = new Vector3(transform.position.x, 4.573845f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    267.         }
    268.  
    269.  
    270.         if (transform.position.y >= 4.573847f) //// if player leaves the screen will pop up on oppisite side
    271.         {
    272.             transform.position = new Vector3(transform.position.x, -4.573845f, transform.position.z); // if player leaves the screen will pop up on oppisite side
    273.         }
    274.     }
    275.  
    276.     #endregion
    277. }
    Looks better like this! :D
     
    Last edited: Mar 24, 2013
  3. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    Is this just a more organized version of the same script? Or did you help with my problem? I'm uploading a webplayer.