void Awake () { if (instance == null) { instance = this; DontDestroyOnLoad (this.gameObject); } else { if (this != instance) Destroy (this.gameObject); } } // Awake ()
// Detecting collisions against the ship using an Unity function. private void OnCollisionEnter(Collision collision) { // Making sure that the collision object is an Asteroid using an `if statement`. if (collision.gameObject.name == "Asteroid_01_S(Clone)") // Detecting the collision between the Ship and Asteroid. { GameObject asteroid = GameObject.FindWithTag("Asteroid"); // Get Asteroid GameObject, to access the Asteroid List. if (asteroid != null) // In case that the Asteroid GameObject was found. { AsteroidScript _asteroidScript = asteroid.GetComponent <AsteroidScript>(); // Get AsteroidScript by Asteroid GameObject. aExplosion = Instantiate(explosion, collision.gameObject.transform.position, Quaternion.identity); // Creating a clone of the explosion and place it in the collision position. _asteroidScript.asteroids.Remove(collision.gameObject); // Removing asteroid from the list to avoid Missing Reference Exception. Destroy(collision.gameObject); // Destroying the asteroid after the collision in the screen. } LoadSceneScript loadScene = new LoadSceneScript(); // Instantiating LoadSceneScript to change scenes. loadScene.ChangeScene(2); // Changing to EndScene after collision. } }