- Home /
transform.find and Instantiate do not return the same type?
Hi, im using a Hashtable for storing new made objects and what groups they belong to. I have created a transform Object called 'Scene' before hand, which also belongs in the Hashtable. The 'Scene' is the main group, later sets will be added to the 'Scene' dynamically.
(dont think to much about the functions, Im still testing things ;) )
function Start(){
var elemData : Hashtable = new Hashtable();
elemData["Transform"] = transform.Find("Scene");
Debug.Log("found:"+elemData["Transform"]); //contList.add(parentObject); <-- the later statement works with var parentObject : Transform; //contList.add(elemData["Transform"]); <-- the later statement doenst work contList.add(elemData);// <-- the later statement doenst work }
the add function exists as add(hashtable) and add(transform), the add(transform) just calls the add(hashtable) variation.
After this I add the Objects to the game (at the moment just by pressing fire, later over user clicks and a database)
function Update () {
if (Input.GetButtonDown("Fire1")){
//var newobj:transform = Instantiate(newObject, transform.position,transform.rotation);
var elemData : Hashtable = new Hashtable();
elemData["Position"] = new Vector3((modWidth*counter*scale),0.0,(modHeight*0.5*scale));
elemData["Scale"] = new Vector3(scale,scale,scale);
elemData["Color"] = new Color(0.5,0.5,0.5,1);
elemData["Translation"] = new Vector3(0.0,0.0,0.0);
elemData["Angle"] = new Vector3(270.0,180.0,0.0);
elemData["Transform"] = Instantiate(newObject,Vector3.zero,Quaternion.identity);
//elemData["Transform"] = parentObject; <-- Also works
counter ++;
contList.add(tmpcnt,elemData);//<-- This works as well
//contList.add(elemData);<-- this works too
if(counter%5==0)tmpcnt++;
Debug.Log(elemData["Transform"]); } }
The counter and tmpcnt is there for grouping only and testing. Now to my problem of understanding.
I am saving the gameObjects in a different class:
this.container = elemData["Transform"];
and testing if the this variable is set with following statement:
if(this.container!=null){...}
this statement is only true when I either use a fixed variable pointing at the transform.Scene, create an object with Instantiate, but false when I use transform.Find. All 3 variations are Objects or not? Later I will be finding the sets dynamically over the name, so I will be using the find, but if this doesnt work, then I will need to plan something else. I wasnt sure if it had something todo with gameObject and transform, but converting the find result into a gameobject/transform didnt help.
I do hope someone can clear my misunderstanding on this point
thankyou Ent
Answer by dhendrix · Jun 09, 2010 at 01:20 PM
Transform.Find returns the transform of the gameObject, while Instantiate returns a reference to the gameObject itself. To compare them you should do transform.Find("whatever").gameObject.
+1. This trips up people a lot - the relationship between a Transform and GameObject, and when each is used, isn't immediately obvious (at least it took me a while to figure it out, but maybe I'm just slow. :)