Destroying all clones using other scripts

Hi, I’m new here. There’s a Unity problem that’s been bugging me for a few days.

I’m trying to make a game interacting with some characters forming a circle. If I click a button, these characters moves away and gets deleted. So that other characters can get in the circle.

However, every time I try to delete these clones, an error appears. Looking like this:
“NullReferenceException: Object reference not set to an instance of an object”

I’ve tried using the gameObject.Find command, but this only allows me to delete one clone when the button is clicked.

Here’s my code:

//Instantiate.cs
public Rigidbody2D alienPrefab ;
	public float Number ;
	public Rigidbody alienInstance;
	public DestroyThem other ;

	void Start () {

		MakePeople ();
	}

	void Update () {

	}

	void OnMouseDown () {
		other.DestroyPeople ();
		//MakePeople ();
	}

	void MakePeople () {
		float Degrees ;
		int i ;

		Number = Random.Range (2, 10);
		Degrees = (2*Mathf.PI)/Number ;
		
		for ( i = 1 ; i < Number ; i++ ) {
			alienInstance = Instantiate( alienPrefab , new Vector3(5*Mathf.Cos (Degrees*i) ,5*Mathf.Sin (Degrees*i),0) , transform.rotation ) as Rigidbody ;
		}
	}


//DestroyThem.cs
public Instantiate ins ;

	public void DestroyPeople() {
		//if ( ins.alienInstance != null)
		//Destroy (gameObject);
		//Destroy (ins.alienInstance);
		//Destroy (GameObject.Find("OtherCharacters2(Clone)").rigidbody );
		Destroy (GameObject.Find ("OtherCharacters2(Clone)")); //<---Here's the problem!
		//Destroy (GameObject.FindGameObjectsWithTag("Others"));
		//DestroyFlag = true;

	}

Hi,

“Null” is an expression for you tried to access and work with an empty position in RAM memory. Meaning, there was nothing there.

I would personally from experience try to do this a bit differently. I would have two scripts in my scene to handle this.

  1. A “group.cs” script that I place on an empty game object in the scene.
  2. A “alien.cs” script that I place on a prefab.

(The “groups.cs” script can also be on a prefab if you want to have several groups appearing in the scene at the same time and got a main game script somewhere)

The group script is responsible for managing the aliens in each group through a list or array. Just instantiate a group that then instantiates the number of enemies that you want. Just run through the list in the group with a for loop when you want to destroy them.

Hi.

I think you can to keep links to your NPC on some list, after creation, and when you’ll be need to destroy them, just “follow” to that links, and do it.

It has been 10 months since I’ve touched Unity. Looking back at this post, I suppose I should reply an answer to this old amateur me.

GameObject.Find() can only find one object at a time, which is the first object counting down from the top of hierarchy.

If you want to destroy all the gameobjects with one call, for instance KeyCode.E. You will have to attach a “destroy code” to the prefab before you want to instantiate it. Then call the “destroy code” with another code.

//Destroy.cs 
//Put this in your prefab before instantiating it
    
void DestroyIt () {
	if ( GameObject.Find("GlobalObjectName").GetComponent<Global>().Kill_it == true )
		Destroy(this) ;
}

The second code:

//Global.cs
//This code controls all the prefabs

public bool Kill_It = false ;
    
void Update () {

	if ( Input.GetKeyDown(KeyCode.E) ) {
		Kill_It = true ;
	}
}