- Home /
making the instantiated game object as a child of the GameObject
Hi, I can I make the gameObject as a child of the another gameOject.... I create the empty gameObject called "GameLevel" and I also instantiate several objects from my prefabs and make them children of the "GameLevel" object. The below code is not working. How can I fix it to get it working..
GameObject objLevel = new GameObject("GameLevel"); for (int i = 0; i < totalMaxObjects.Count; i++) { for (int j = 0; j < totalMaxObjects[i].position.Count; j++) { Vector3 euler = totalMaxObjects[i].rotation[j]; Quaternion rot = Quaternion.EulerAngles(euler);
if (objectTypes[i] != null)
{
GameObject obj = Instantiate(objectTypes[i], totalMaxObjects[i].position[j] * GlobalScale, rot) as GameObject;
obj.transform.parent = objLevel.transform;
}
else
{
Debug.Log("Transforms missing");
break;
}
}
}
Answer by Loius · Nov 13, 2010 at 04:16 AM
What exactly is the problem?
All I can see right now is that Quaternion has no EulerAngles function, but you should have gotten a compile error from that.
GameObject obj = Instantiate(objectTypes[i], total$$anonymous$$axObjects[i].position[j] * GlobalScale, Quaternion.identity) as GameObject;
Quaternion is not the problem... I tried with Quaternion.identity and it doesn't work either... The problem is that "NullReferenceException: Object reference not set to an instance of an object" and O remove obj.transform.parent = objLevel.transform; and it works perfect... so there might be problem with objLeve.transform
Null Reference Exception means something doesn't exist and you're trying to access it. Try using objLevel.AddComponent(Transform), or having the GameLevel exist before running the script.
Thanks.. it was the problem with ObjLevel...I don't know what's wrong with the first time creation of the gameobject in script..
if(ObjLevel) obj.transform.parent = objLevel.transform; and it works..