- Home /
I'm trying to convert a Transform to a GameObject (?)
Hi. Thanks for reading this. Listen, I'm new to Unity and hope you pardon my possibly stupid question. Here's the breakdown:
I instantiate an instance of Drone. Then I push drone into a globalArray. When drone is pushed, it is a Transform object. Here's code:
var droneAmount:int = 10; // When a collision happens destroy ourselves // and spawn an explosion prefab instead
for(var i=0; i<droneAmount; i++){ var drone : Transform; var explosion : Transform; explosion = GameObject.Find("plDrone").transform; drone = Instantiate(explosion, transform.position, transform.rotation); drone.name = "drone" + i; global.enemyList.push(drone); global.enemyNames.push(drone.name); print("INDIVID ENTITY " +drone);
So, this drone has health. I fire bullets that have colliders setup so OnTriggerEnter, it depletes drone's health. When the drone reaches 0 health, I destroy the child, and splice the info from appropiate arrays. Here's code:
rigidbody.freezeRotation = true; var health:int = 10; var go:GameObject;
function Start(){ iTween.moveTo(go,{"x":-3.634033, "y":29.86236, "z":70.46572, "time":10}); iTween.rotateTo(go,{"x":270, "y":270, "z":0}); } function Update () {
//print(health); if(health<=0){ var child : GameObject = GameObject.Find(name); print(child); Destroy(child); var shootOff : GameObject = GameObject.Find("plTurrent"); var com0 = shootOff.GetComponent("SmoothLookAt_VarTarget"); com0.enabled = false;
for(i=0; i<global.enemyNames.length; i++){ if(child == global.enemyNames[i]){ global.enemyNames.splice(i, 1); global.enemyList.splice(i, 1); } }
} }
On Line 21 of last code, "if(child == global.enemyNames[i]){", I know I'm not doing something right. I think I need to convert a Transform to a GameObject. Sorry if there's something glaringly obvious. Tired and stuck. Thanx.
Answer by Jessy · Apr 22, 2010 at 06:54 AM
I'll look at the code if this simple answer doesn't do it for you; I'm just going to answer the main question you're asking: You do not "convert" a Transform to a GameObject. Transform is a Component that gets attached to a GameObject. You can access the GameObject by using transform.gameObject. ".gameObject" works with any type of Component.
http://unity3d.com/support/documentation/ScriptReference/Component-gameObject.html
ok another question then: how to get a gameobject from transform.find ?? a: transform.Find("A").gameObject;
So basically if anyone has cloned a bunch of robots and wants to select them
if (mousePressed) {
if (hit.collider.name == "Drone(Clone)") {
if (lastSelector != hit.collider.gameObject.transform.Find("DroneSelector").gameObject){
selectedObject = "Drone";
hit.collider.gameObject.transform.Find("DroneSelector").gameObject.GetComponent($$anonymous$$eshRenderer).enabled = true;
if (lastSelector != null) lastSelector.GetComponent($$anonymous$$eshRenderer).enabled = false;
lastSelector = hit.collider.gameObject.transform.Find("DroneSelector").gameObject;
}
} else
Answer by Rickasheye · Jun 06, 2016 at 05:10 AM
easy just put the game object name then the thing .transform gameObject_Name.transform then ladada
Yes - which is exactly what @Jessy wrote above, over six years ago...