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
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.