Search Unity

Lives sytem

Discussion in 'Getting Started' started by PetterL68, Feb 24, 2019.

  1. PetterL68

    PetterL68

    Joined:
    Jun 26, 2018
    Posts:
    23
    I have been trying to get a life system to work in my fist small game. But have now made a small test to see if i can get it to work. But no luck yet.

    The test is just a ball and a kube object. If ball run into the cube. The ball dies (destroyed)
    Wait a few sec and then be respawned.
    For now the ball dies but not appear again after a few sec. I get a message that i still trying to use a destroyed object
    Game Controller:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameController : MonoBehaviour
    7. {
    8.     public GameObject GameOver;
    9.  
    10.    
    11.     public Text text;
    12.     public Player Player;
    13.     public Transform Spawn;
    14.     //public Transform playerShip;
    15.     private static bool dead;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         Player.Lives = 3;
    20.         // text.text = " ";
    21.         // Instantiate(Player);
    22.         Spawn = GameObject.FindGameObjectWithTag("Spawn").transform;
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (Player.Lives >= 3) Player.Lives = 3;
    29.  
    30.         if (Player == null && Player.Lives >= 1)
    31.         {
    32.             Player.Lives--;
    33.             StartCoroutine(Example());
    34.          
    35.                 Instantiate(Player, Spawn.transform.position, Quaternion.identity);
    36.  
    37.        
    38.     }
    39.    
    40.     IEnumerator Example()
    41.     {
    42.         yield return new WaitForSeconds(5);
    43.     }
    44.  
    45. }
    46.  
    Player

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.     float dirx, diry;
    10.     Rigidbody rb;
    11.     public float Speed = 2f;
    12.     public static int Lives;
    13.  
    14.     // Start is called befo;re the first frame update
    15.     void Start()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         dirx = Input.GetAxis("Horizontal");
    24.         diry = Input.GetAxis("Vertical");
    25.     }
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         rb.velocity = new Vector3(dirx * Speed, diry * Speed,0);
    30.     }
    31. }
    32.  
    Lifesystem

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class LivesScript : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.        
    16.     }
    17.     private void OnTriggerEnter(Collider other)
    18.     {
    19.         // GameController.text  = "AUUUUU";
    20.         Debug.Log("AUUUU")
    21.  ;      Player.Lives -= 1;
    22.         Destroy(other.gameObject );
    23.     }
    24. }
    25.  
    26.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What line of code and what script has the error?
     
  3. PetterL68

    PetterL68

    Joined:
    Jun 26, 2018
    Posts:
    23
    Not give any specific Line number.. But when clicking on the error message it comes to The gamecontroller Line 35.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Then somewhere in your code you are destroying Player. I can't tell if Player is a scene object, or a prefab, but the way you should do this is you have a Player prefab and instantiate it.


    Also, where are you getting your reference to Player on line 21 in LivesScript? That looks like it should be producing a compile error as written.
     
  5. PetterL68

    PetterL68

    Joined:
    Jun 26, 2018
    Posts:
    23
    Last thing first. The Player.lives refer to the player script Lives.

    Im a bit new to everything yet.

    Have not made the object Prefab that this was just a setup to test out a Lives system.
    In the main program they are prefab
    .
    Edit: I removed the destroy in livesript and put it above the instanciate in GameController.
    Destroy(this.gameobject); But now the player does not get destroyed when it hits enemy. But it trigger the event. Have a Debug.log telling me so.
     
    Last edited: Feb 26, 2019