- Home /
Transform child object over time and then destroy it.
I have a parent object which contains a bunch of instantiated children
  gameObjectChild = Instantiate(Resources.Load(spawnObjName), pos, Quaternion.identity) as GameObject;
             gameObjectChild.name = "child" + objectName++;
             gameObjectChild.transform.SetParent(parent.transform, true);
Parent is transforming with his children each time the button is pressed.
 GameObject.Find("parent").transform.position += new Vector3(0, -0.94f, 0);
Now I wish to move over time by X axe and then destroy a child object each time the button is pressed. I am able to destroy it with this:
                     Destroy(GameObject.Find("child" + destroyObjectName++), 0.02f);
Unfortunately I can't menage to transform it over time properly. I tried with this, but it just messes everything up.
 GameObject.Find("child" + destroyObjectName++).transform.locaposition += new Vector3(1, 0, 0);
Is this the right approach to do that? Any ideas? Thanks!
Answer by team_eden · Jan 27, 2015 at 05:51 PM
For starters
  gameObjectChild.transform.SetParent(parent.transform, true);
should be
  gameObjectChild.transform.parent=parent.transform;
Instead of referencing the parent constantly, do it once, probably on Awake
 // Set a parent object, instead of using find every cycle
 function Awake()
 {parentObject=GameObject.Find("parent");}
 
 //Move parent on click & also destroy a child on click
 function OnMouseClick()
 {parentObject.transform.position += Vector3(0.0, -0.94f, 0.0);
  // On your old destroy line you had "child" when it should be "parent/child"
  // Because in the hierarchy it should literally be the child of the parent
  // searching your hierarchy for Every object named "child" is bad practice.
  // Also, should not be incrementing variables during a command. Do it before
  destroyObjectName++;
  Destroy(GameObject.Find("parent/child" + destroyObjectName), 0.02f); } 
 
 
 //Move over time by x
 function Update()
 {parentObject.transform.position+=Vector3(1.0*Time.deltaTime, 0.0,0.0);}
This last line is also incorrect, even if the logic was correct too
 GameObject.Find("child" + destroyObjectName++).transform.locaposition += new Vector3(1, 0, 0);
Should be
 destroyObjectName++; //increment first
 GameObject.Find("parent/child" + destroyObjectName).transform.localPosition += Vector3(1.0, 0.0, 0.0);
Hope that helps.
Your answer
 
 
             Follow this Question
Related Questions
Problem with making child an object 2 Answers
Position of parent-Object 2 Answers
Make a simple tree 1 Answer
Camera follow ball along cylinder 1 Answer
Moving parent to position of child 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                