[Solved] Game doesn't switch to Win Scene if player dies

Hello,
As mentioned in the title, the game doesn’t switch to the Wii scene if the player dies.
The Die function works (The player disappears and i have a sound effect that plays when the player dies), but the rest of it doesn’t work, instead of moving to the Win Scene Unity gives me an error message in the console: “NullReferenceException: Object reference not set to an instance of an object”

When i click on the error massage it takes me to the second half of the Die function.
Below is the script of the Die function.

void Die ()
	{
	Destroy (gameObject);
	AudioSource.PlayClipAtPoint (InfinitySound, transform.position);
	LevelManager man = GameObject.Find("LevelManager").GetComponent <LevelManager>();
	man.GotoScene ("Win Scene");
	}

Thank you

You are destroying your gameobject before calling component on it.
First do your things then destroy the gameobject

Hello Dieedi,
Thank you for the reply.
I switched the order of things in the Die() function. But i still got the same error. And now the player won’t die, i am guessing because the function doesn’t reach the die command at the bottom, it i instead getting stuck on the first line.
I am attaching a snapshot of the error below.

Thank you very much

Can you post the whole script please ?

Ok more details about your Die method here, your Die() method belongs to your class (your cs file) and your class/file is attach to a GameObject. When you use Destroy(gameObject) , you are referencing the gameObject on which is your script attach to, so once you call Destroy on it , it disappear and can’t execute the lines after it.

That’s why the whole script is important to check to find a way of debug/improve.

one more thing, ‘gameObject’ reference the GameObject on which the script is attach to. When you call transform.position it’s the transform component of the GameObject on which the script is attach to.

Hello again,
Sorry for the late reply. Please find the script for the player script (i named the player: Clover)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class Clover : MonoBehaviour {

public float speed = 15f;

public float health = 150f;

float MaxX;

float MinX;

float Padding = 0.5f;

public GameObject LaserFist;

public float LaserSpeed = 7f;

public float FiringRate; 

public AudioClip InfinitySound;

    // Use this for initialization

    void Start () {

    float distance = transform.position.z - Camera.main.transform.position.z;

    Vector3 leftmost = Camera.main.ViewportToWorldPoint (new Vector3(0,0,distance));

    Vector3 rightmost = Camera.main.ViewportToWorldPoint (new Vector3(1,0,distance));

    MinX = leftmost.x;

    MaxX = rightmost.x - Padding;

    }

    void Fire () {

        Vector3 offsef = new Vector3 (0,1,0);

        GameObject laser = Instantiate(LaserFist, transform.position + offsef, Quaternion.identity) as GameObject;

        laser.GetComponent<Rigidbody2D>().velocity = new Vector3 (0,LaserSpeed,0);

    }

    // Update is called once per frame

    void Update ()

    {

        if (Input.GetKeyDown (KeyCode.Space)) {

            InvokeRepeating ("Fire", 0.01f, FiringRate); 

        }

        if (Input.GetKeyUp (KeyCode.Space)) {

        CancelInvoke ("Fire");

        }

        if (Input.GetKey(KeyCode.LeftArrow))

         {

        // this part (speed * Time.deltaTime) makes the movement frame independant

             //transform.position += Vector3.left * speed * Time.deltaTime; use THIS Or:

             transform.position += Vector3.left * speed * Time.deltaTime;

         }

         if (Input.GetKey(KeyCode.RightArrow))

         {

         // this part (speed * Time.deltaTime) makes the movement frame independant

            //transform.position += Vector3.right * speed * Time.deltaTime; use THIS Or:

            transform.position += Vector3.right  * speed * Time.deltaTime;

         }

         // To restrict the player with in the game space. In 2D games Vector2 can be used instead of Vector3

         float newX = Mathf.Clamp (transform.position.x, MinX,MaxX);

         transform.position = new Vector3 (newX, transform.position.y, transform.position.z);

      

    }

    void OnTriggerEnter2D (Collider2D collider)

    {

        Projectile missile = collider.gameObject.GetComponent <Projectile> ();

        if (missile) {

            health -= missile.GetDamage ();

            missile.Hit ();

            if (health <= 0) {

            Die();

            }

            print ("We Are hit!"); 

        }

}

void Die ()

    {

    LevelManager man = GameObject.Find("LevelManager").GetComponent <LevelManager>();

    man.GotoScene ("Win Scene");

    Destroy (gameObject);

    AudioSource.PlayClipAtPoint (InfinitySound, transform.position);

    }

}

Do you have a level manager, called LevelManager, active in your scene? The error could simply come from a difference in spelling.

1 Like

Ok it’s like I’ve said before, your Die() method is trying to destroy your current gameObject which is your Player !

Why did you want to destroy it ? Or, what do you really want to destroy ?

1 Like

Just to add, the characters you need to add to format the code are actually these `, you need three of them before the code, and then three of them after the code.

I have corrected the posts above for readability.

Hope this helps :slight_smile:


See also;

Hello Sebastian_Martens,
Yes i do have one attached to the player

1 Like

Thank you Rob :slight_smile: , sorry i keep making the same mistake

1 Like

Hello Dieedi,
Yeah i guess destroying the player isn’t necessary when if i want to switch over to another scene if the player’s health reaches 0 or below. So i tried deactivating the destroy command like so:


void Die ()
	{
	LevelManager man = GameObject.Find("LevelManager").GetComponent <LevelManager>();
	man.GotoScene ("Win Scene");
//	Destroy (gameObject);
//	AudioSource.PlayClipAtPoint (InfinitySound, transform.position);
	}

}

but i still didn’t get the result i want (The game doesn’t switch to the win scene)

1 Like

You don’t have a GameObject called “LevelManager” in your scene your actual LevelManager script is attached to a gmaeobject called “Clover Defender”. So as @Sebastian_Martens pointed it , your script can’t find the Levemanager :wink:

You should create an Empty Object called “LevelManger” with your LevelManager script attached to or change your line to GameObject.Find(“Clover Defender”).GetComponent(); But I think the GameObject called LevelManager is a better solution.

2 Likes

Oh! I see…
Now that you mention it, I feel like I should have spotted it a mile away
Thank you Dieedi :slight_smile:

1 Like

It’s no problem, it’s really just a case of helping you to help yourself, and those that subsequently help you :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms