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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameController : MonoBehaviour { public GameObject GameOver; public Text text; public Player Player; public Transform Spawn; //public Transform playerShip; private static bool dead; // Start is called before the first frame update void Start() { Player.Lives = 3; // text.text = " "; // Instantiate(Player); Spawn = GameObject.FindGameObjectWithTag("Spawn").transform; } // Update is called once per frame void Update() { if (Player.Lives >= 3) Player.Lives = 3; if (Player == null && Player.Lives >= 1) { Player.Lives--; StartCoroutine(Example()); Instantiate(Player, Spawn.transform.position, Quaternion.identity); } IEnumerator Example() { yield return new WaitForSeconds(5); } } Player Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { float dirx, diry; Rigidbody rb; public float Speed = 2f; public static int Lives; // Start is called befo;re the first frame update void Start() { rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update() { dirx = Input.GetAxis("Horizontal"); diry = Input.GetAxis("Vertical"); } private void FixedUpdate() { rb.velocity = new Vector3(dirx * Speed, diry * Speed,0); } } Lifesystem Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class LivesScript : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private void OnTriggerEnter(Collider other) { // GameController.text = "AUUUUU"; Debug.Log("AUUUU") ; Player.Lives -= 1; Destroy(other.gameObject ); } }
Not give any specific Line number.. But when clicking on the error message it comes to The gamecontroller Line 35.
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.
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.