Destroy Player(Object), Instantiate to get Prefab, getting ArgumentException: The Object you want to

Hello,
I have been doing the SpaceShooter game from Unity and completed, but not trying to improve and learn more about the process of building a game. When I call an OnTrigger it works kind of, but when I subtract a life from my player, it actually subtracts 2 the first time. Here is the trigger:

    void OnTriggerEnter(Collider other)
    {
        //Debug.Log(other.name);
        //if(other.tag == "Boundary" || other.tag == "Enemy")
        if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy"))
        {
            return;
        }

        if(explosion != null)
        {
            Instantiate(explosion, transform.position, transform.rotation);
            //ad.Play();
        }

      

        if(other.CompareTag ("Player"))
        {
           
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gamecontroller.GameOver();
           
        }

        gamecontroller.AddScore(scoreValue);
        Destroy(other.gameObject);
        Destroy(gameObject);

Then in the Game controller I remove the life, which starts at 3. The UpdateScore(), just updates GUIText. Any ideas wahy the count is not working correctly.

public void GameOver()
    {
         life = --life;
         UpdateScore();

         if (life > 0)
        {

        Instantiate(Resources.Load("Player"));         
 
        }

        if (life <= 0)
        {

            gameOverText.text = "Game Over";
            gameOver = true;
        }
    }

Here is the other Error I am getting: This is line 49 of DestroyByContact.cs( gamecontroller.GameOver():wink:

ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239)
UnityEngine.Object.Instantiate (UnityEngine.Object original) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:176)
GameConroller.GameOver () (at Assets/Scripts/GameConroller.cs:59)
DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroyByContact.cs:49)

To fix the error:
I’m going to take a wild guess and say that explosion, playerExplosion, gamecontroller, or scoreValue are not defined.

About the health thing:
Maybe two things are calling GameOver and decreasing your health.
Also when you do ‘life = --life;’ you can just do ‘life–;’

1 Like

Thanks for the response, I took a look and I do have them defined I believe. Below is where I declare the values, if I am doing it wrong, please let me know. Then in the Inspector I did not have values, so I added, but still get the error. I removed the Life stuff for now, until I can fix this issue.

    public GameObject explosion;
    public GameObject playerExplosion;
    public GameObject Player;
    public int scoreValue;
    private GameConroller gamecontroller;

2936082--217162--upload_2017-1-26_8-2-23.png

Ran again and still get error:
ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239)
UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:201)
DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroyByContact.cs:54)

Check the values in the Inspector for the object(s) in the scene that has this script attached to it.

1 Like

Can you tell us what line 54 is in DestroyByContact? The errors always tell you where the error is happening. Then you just gotta figure it out.

1 Like

I went through each object that uses the DestroyByConact file and added the reference(Might be wrong term), to the Prefab player and all other items that show up in the inspector, still same error.

Live 54 has the following: (LINE 54 is Instantiate(Player):wink:

So I load a new instance of Player from the Prefabs, then instantiate, the player does show up, but in the Console getting that error message.

        if(other.CompareTag ("Player"))
        {
           
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);

            Player = Resources.Load("Player") as GameObject;
            Instantiate(Player);
            //gamecontroller.GameOver();
  
        }

If you’re assigning Player via Inspector why are you trying to load it from Resources?

1 Like

lol you changed the code on us. That’s why I couldn’t find the error. Kelso is correct. You dont need to load it from Resources as you already have the prefab linked. You can remove the ‘Player = Resources.Load…’ and just have ‘Instantiate(Player)’ as you loaded the Player unless the Resources.Load(“Player”) is something else than the GameObject player. then I suggest you call them something different. It also seems like maybe ‘resources.load(player)’ doesn’t exist.

1 Like

THANK YOU!!! Sorry for the change in code, I was messing around and trying to figure it out. Google has a lot of OLD info. :slight_smile: Removed the resource.load fixed it. again, thank you for taking the time to respond juicyz and KelsMRK.