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

Lives not going away

Discussion in 'Scripting' started by Zero101, Apr 19, 2015.

  1. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    I want to make "LiveIcon" systme where if the player is deleted (is made in another script) the number of lives will be reduced and an icon will be destroyed but when the player dies the first time, nothing happends , the scond time, the first and second icon get destroyed and third time , the third one dies too. Why is not, the first time ,the first icon not dieing? Here is the code for the Icon (I have separate ones for the 2nd and 3rd one but it is just diffrent in the way that in witch order they should die )




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Live1IconSpawn : MonoBehaviour {
    5.  
    6.  
    7.  
    8.     public GameObject LivePrefab;
    9.  
    10.     GameObject LiveIcon;
    11.  
    12.     public int numLives = 2;
    13.  
    14.     float respawnTimer;
    15.  
    16.  
    17.  
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.     }
    23.  
    24.     void SpawnIcon() {
    25.         numLives--;
    26.         respawnTimer = 3f;
    27.         //LiveIcon = (GameObject)Instantiate(LivePrefab, transform.position, Quaternion.identity);
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update () {
    32.         if (GameObject.Find("Main_Player_LVL1") == null && numLives > 0) {
    33.             respawnTimer -= Time.deltaTime;
    34.         }
    35.         if (respawnTimer <= 0) {
    36.             SpawnIcon();
    37.         }
    38.         if (numLives <= 0) {
    39.             Destroy (gameObject);
    40.         }
    41.  
    42.     }
    43. }
     
  2. frankrs

    frankrs

    Joined:
    Aug 29, 2009
    Posts:
    300
    Are you destroying the game object that this script is attached to before it can execute this code? Also your find object by name in the update function is going to be very expensive computationally, might consider another way around it.
     
  3. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    Maybe by tag? Other way I can not imagine because it needs to be on update because if not all the livea would go away from start.And no, if the player ship was destroyed the object would destroy the object, but there is a problem where it doesn't find the ship before it appears (I have a script where after 3 seconds the player ship would spawn) so somehow the game object ia not detecting the player die...
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    in this design you are checking every frame if the player has been destroyed, in that check you are looking up the player object every time... it would make a lot more sense to have this driven by the player being destroyed, using something like OnDestroy() (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html) you can run a SendMessage(), trigger and Event or whatever so the player object informs the game manager object that they've died and it should update the lives count/whatever.
     
  5. Zero101

    Zero101

    Joined:
    Apr 18, 2015
    Posts:
    14
    Thank you that helped me :)