- Home /
Set Parent EditorScript
This is a snip of my code. I am using Editor script to instance obj into the scene. My objects that I am instancing are to replace objects that are already placed. It grabs their positioning and rotation and scale.
The last thing it is supposed to do is grab the parent of the object its replacing then make itself a child before removing the object in question. Everything works except the parenting. Below is my code. let me know if anyone has had any luck.
My usings using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic;
Script Type : EditorWindow
if (GUILayout.Button ("Set_Tile")) {
object[] obj = GameObject.FindGameObjectsWithTag (ChoiceTag);
foreach (object o in obj) {
GameObject g = (GameObject)o;
Transform q = g.transform;
Position = g.transform.position;
if (DistributeOBJ1) {
n = 1;
} else {
n = TH.RN ();
}
switch (n) {
case 1:
Obj1.transform.position = Position;
Obj1.transform.rotation = g.transform.rotation;
Choose = Obj1;
break;
case 2:
Obj2.transform.position = Position;
Obj2.transform.rotation = g.transform.rotation;
Choose = Obj2;
break;
case 3:
Obj3.transform.position = Position;
Obj3.transform.rotation = g.transform.rotation;
Choose = Obj3;
break;
case 4:
Obj4.transform.position = Position;
Obj4.transform.rotation = g.transform.rotation;
Choose = Obj4;
break;
case 5:
Obj1.transform.position = Position;
Obj1.transform.rotation = g.transform.rotation;
Choose = Obj1;
break;
case 6:
Obj2.transform.position = Position;
Obj2.transform.rotation = g.transform.rotation;
Choose = Obj2;
break;
case 7:
Obj3.transform.position = Position;
Obj3.transform.rotation = g.transform.rotation;
Choose = Obj3;
break;
case 8:
Obj1.transform.position = Position;
Obj1.transform.rotation = g.transform.rotation;
Choose = Obj1;
break;
case 9:
Obj1.transform.position = Position;
Obj1.transform.rotation = g.transform.rotation;
Choose = Obj1;
break;
case 10:
Obj2.transform.position = Position;
Obj2.transform.rotation = g.transform.rotation;
Choose = Obj2;
break;
}
GameObject clone=null;
clone=PrefabUtility.InstantiatePrefab (Choose) as GameObject;
(GameObject)clone.transform.parent=g.transform.parent;
//DestroyImmediate (g);
}
}
I am trying to have my EditorScript get all tagged items into an array, then set the x,y,z of the
Add-Info
What currently happens is as follows-
Setup- GameObject(parent) Child(target)
Code runs before destroying g I set the parent of clone to = g's parent. clone=PrefabUtility.InstantiatePrefab (Choose) as GameObject; (GameObject)clone.transform.parent=g.transform.parent; Child is = g. in the code I grab g's parent then destroy g.
the result is- g is destroyed clone is created clone has no parent
Answer by Belangia · Oct 22, 2015 at 08:11 AM
Answer: clone=PrefabUtility.InstantiatePrefab (Choose) as GameObject; creates the prefab as it is in the game folder. Since it has no parent, the file does not parent when being created.
Revert back to using Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity); which creates a clone or instance rather than the original. This allows you to edit the parenting relationship once the object has been created within code.