Hey!
Sorry for the following noobish question. How can I replace an object with another (or remove it’s properties / components and give them those from the new object)? If I just make obj1.GameObject = obj2.GameObject it says that is read only.
Hey!
Sorry for the following noobish question. How can I replace an object with another (or remove it’s properties / components and give them those from the new object)? If I just make obj1.GameObject = obj2.GameObject it says that is read only.
Destroy one obj and in the OnDestroy instantiate the new one.
Like
Destory(gameObject);
void OnDestroy()
{
Instantiate(objnew,this.transform…)
}
This obviously isn’t a working code and just an example
Hmm… I tried with
var objNew : GameObject = new GameObject();
objNew = sparks.k6; // this is the new desired game object
objNew.name = "SparkEffect";
Destroy(GameObject.Find("SparkEffect"));
Instantiate(objNew, Vector3.zero, Quaternion.identity);
objNew.transform.parent = transform;
and it says
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
UnityEngine.Transform:set_parent(Transform)
Also, it seems the new object cannot be renamed and by this method I get duplicated objects. The new GameObject is in a script which is on an item you pick from floor so that is why it’s a prefab.
bumb ![]()
If I try with
var t : Transform;
foreach (t in GameObject.Find("SparkEffect").GetComponentsInChildren(typeof(Transform)))
{
var newObject : GameObject;
newObject = (GameObject)EditorUtility.InstantiatePrefab(sparks.k6);
newObject.transform.position = t.position;
newObject.transform.rotation = t.rotation;
Destroy(t.gameObject);
}
it says I need to insert semicolon on foreach line??? wtf?
Hehe no one knows? :))