Destroy specific clone

I have looked over some of the other questions related to this but can’t seem to find one that actually fits my situation. I figure it will be simple and i’m just not seeing it.

In my game, the player clicks to instantiate player prefab objects. If those player objects collide with certain other objects, i would like to destroy that particular player object.

Currently, if any of the player objects collide, the player is no longer able to click and instantiate new player objects and i get the following:

MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.

and

MissingReferenceException: The object of type ‘Rigidbody’ has been destroyed but you are still trying to access it.

This is in the script for the non-player object

 void OnTriggerEnter(Collider other)
    {
            Destroy(other);
    }

You should create a script that adds force and put on your prefab!

  • You do not need that on update just on Awake will do the job
  • Once it is instantiated it will enter the Awake ()

-Like this

void Awake() {
        rigid = gameObject.GetComponent <Rigidbody>();
	rigid.useGravity = false;
	rigid.isKinematic = false;

	rigid.AddForce (transform.forward *speed); //****
 }
  • On the gameObject that shoots (player) you check on Update the click
  • AND instantiate
  • Once it is instantiated it will enter the Awake () and add the force to the prefab

Hope it works!

The following solves my problem of some objects remaining in a isKinematic state after i release the mouse button. I moved all button checks from FixedUpdate to Update with a boolean check in fixedupdate to prevent forces being applied multiple times.

private bool pforceApplied = true;    

void Update()
    {
        // Place Planets
        if (Input.GetMouseButtonDown(0))
        {
            positionOne = FindPosition();
            playerPlanet = Instantiate(planet, positionOne, Quaternion.identity) as GameObject;
            prb = playerPlanet.GetComponent<Rigidbody>();
            prb.isKinematic = true;
        }

        // Calculate force applied to planet
        if (Input.GetMouseButtonUp(0))
        {
            CalcForce();
            pforceApplied = false;
            prb.isKinematic = false;
        }
}

    void FixedUpdate()
    {        
        // Apply force and launch planet
        if (pforceApplied == false)
        {
            prb.AddForce(force);
            pforceApplied = true;
        }
}

To fix my problem with the clones being destroyed with no ability to create new ones, i did the following. Which works with out issue so far. on occasion it throws an error that last briefly then clears it self. not totally sure why yet.

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour {

    // Destory any object that collides with this one
    void OnTriggerEnter(Collider other)
    {
    Destroy(other.gameObject);
    }

}

I never really had luck adding the AddForce script and method to the actual player object. It would apply the force but then apply the force to every object the player instantiated. The current fix allows the player to instantiate the object with the mouse, draw back and release applying an initial force only.

I am trying to find a way around a similar issue, and I know that the error: MissingReferenceException: The object of type ‘Rigidbody’ has been destroyed but you are still trying to access it. is because it is deleting the first instantiated object, but is still keeping track of how many have been created. Therefore it shows that a gameobject is missing(behind doors), and tries to keep accessing it IN ORDER :/. I know that is not much help but it is something I found out that has some relation to an original issue you had.

I am trying to figure as well how to fix this by deletion on my own terms, and not in order of creation like Unity is forcibly doing.