Solved: Instantiate gameObject and destroy clone on click

Hi,

i want to Instantiate GameObjects from prefabs by a List. This are some kind of units, which will have stats and can do some actions.

When one Clone is clicked, only this Clone should get destroyed or what the ever.

By now, every Clone gets destroyed on click.

I’ve seen some scripts which checks the RayCast of the mouse but

a) i dont get it working

b) its seems to be wrong to do this like this :wink:

function SpawnUnits() {

	var unitData = JSON.Parse(_netCode.GetUnitData());
	
	var factions = unitData["units"].Count;
	var units : int;
	var _unitHandlerGO = Resources.LoadAssetAtPath("Assets/prefabs/UnitHandler.prefab", typeof(GameObject));	
	
	var unit = 0;
	var c = new Array();
	c[0] = Color.blue;
	c[1] = Color.red;
	for (var i : int = 0;i < factions;i++) {
		units = unitData["units"]*.Count;*
  •  for (var k : int = 0;k < units;k++) {*
    
  •  	//Debug.Log(unit);*
    
  •  	var _unitHandler : GameObject;*
    
  •  	var _unitHandlerScript : Unit;	*
    

unitHandler = Instantiate(unitHandlerGO, new Vector3(parseInt(unitData[“units”][k][“position”][“x”]) * tileSize, 0, parseInt(unitData[“units”][k][“position”][“z”]) * tileSize), Quaternion.identity);
* unitHandler.name = “unit”+unit;
_unitHandler.transform.Find(“Unit Model”).GetComponent.().material.color = c;
unitHandlerScript = unitHandler.GetComponent.();
unitHandlerScript.Init(i, unit, unitData[“units”][k][“name”], unitData[“units”][k][“type”], unitData[“units”][k][“melee”], unitData[“units”][k][“ballistic”], unitData[“units”][k][“health”]);*

* unit++;*

* //Destroy(unitHandler);*
* }*

* }*

}
And from Script, which is attached to the prefab which get spawned.
function Update() {_

* if(Input.GetMouseButtonDown(0)) {*
* //var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);*
//var hitInfo : RaycastHit;
* //var collider : Collider = GetComponent.();*
*//Debug.Log(Physics.Raycast(ray, hitInfo, Mathf.Infinity)); *
* //if(Physics.Raycast(ray, hitInfo, Mathf.Infinity)) {*
* //Destroy(gameObject);*
* Debug.Log(this.name);*
* //Debug.Log("Selected: " + id + " " + name);*
* //}*

* }*
}
So, if there is some bad practice in, please tell me also. :wink:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test : MonoBehaviour {
	public GameObject InstantiatedEnemy;//objects which holds //the newly created enemy
	public List <GameObject> EnemiesList;//list which contains enemies
	int ReqEnemyNo;//

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
//Creates the enemy at required position,enemy is loaded from EnemiesList
		if(Input.GetKeyDown(KeyCode.Return))
		{
			InstantiatedEnemy = (GameObject) Instantiate(EnemiesList[ReqEnemyNo],required position,Quaternion.identity);

		}
		if(Input.GetKeyDown(KeyCode.Mouse0))//on mouse click
		{
			Destroy(InstantiatedEnemy);
			
		}

		

	
	}
}

this is untested code.
The issue with your code is that all enemy prefabs have the destroy script so same code runs for all the enemies.Henceforth, all enemies gets destroyed as you have attached the same code which destroys prefabs on mouse click.

Hey,

the trick was to use OnMouseDown, which basicly is a RayCast.

Thx for your help.