[Solved] Coding Problem w/ instantiating and Destroying Objects?

Hello All,

I am having multiple problems with the following code. And I really need some fresh eyes to see to look over the code and tell me what I am doing wrong, because I can’t see it and I’m not that experienced with Unity and how to structure things correctly:

  • The most obvious problem is that when the health is zero or less than zero the main gameObject will not be destroyed.
  • The clone object doesn’t get destroyed after 1.5 seconds are over, it just sticks around in the Heirarchy.
  • The powerup will not instantiate.

The only other relevant information is that myPrefab and explodeBaddy are located at the top of the variable list with:

  • public GameObject myPrefab;
  • public ParticleSystem explodeBaddy;

linked to the appropriate objects.

The code is here:

void OnTriggerEnter2D(Collider2D col)
    {
        //print ("Enemy has been hit by: " + col);
       
        if (col.gameObject.tag == "myLaser")
        {
            //print("BaddyShip hit by laser");
            nbp_health -= 100;

            if(nbp_health <= 0)
            {
                GameObject clone=(GameObject)Instantiate (explodeBaddy,
                                                          transform.position,
                                                            Quaternion.identity);

                GameObject go = (GameObject)Instantiate(myPrefab,transform.position,
                                                        Quaternion.identity);
                Destroy(gameObject);
                Destroy (clone,1.5f);
            }
        }

    }//end function

To anyone who can help here is a big thankyou in advance!

Regards.

I wasn’t paying close enough attention to the console box :(. There was a, type cast mismatch, for reasons I can’t fathom, probably from tiredness, anyway I re-linked the needed items in the Inspector and everthing is behaving correctly now.

Sorry about the post.