- Home /
Parenting MOVING objects during RUNTIME C#
public bool platformMaxed = false;
public static int platformSpawned;
public GameObject PlatformPre;
public GameObject ParentObject;
public Transform platformChild;
void Start ()
{
StartCoroutine(PlatformSpawning());
}
public IEnumerator PlatformSpawning()
{
while (platformMaxed ==false)
{
for (platformSpawned = 0 ;platformSpawned <= 3; platformSpawned++)
{
Instantiate(PlatformPre,
new Vector3(Random.Range(-15,15),Random.Range(1,15),0),
Quaternion.identity);
Debug.Log("Platform Spawned" + platformSpawned);
if (platformSpawned >= 3)
{
platformMaxed = true;
break;
}
}
if (platformMaxed == true)
yield break;
yield return 0;
}
}
public void SpawnSinglePlatform()
{
Instantiate (PlatformPre,
new Vector3(Random.Range(-15,15),Random.Range(1,15),0),
Quaternion.identity);
Debug.Log("Platform Spawned" + platformSpawned);
}
void Update ()
{
}
What I need is, when the object is instantiated it gets parented to a Rotating Sphere object.
I can NOT use: Transform platformChild; platformChild = Instantiate(PlatformPre, New Vector3...) as Transform; As that will give me the following error: Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
Alternatively, how would I go about rotating an object based on the pivot of another object?
Ah damn, fixed it! Just had to make the ParentObject and PlatformPre Objects Transform ins$$anonymous$$d of Gameobject. Doesn't solve the Setting the Parent of a transform error though...
Answer by Owen-Reynolds · Jun 02, 2013 at 05:20 AM
The error says you're trying to change the raw prefab, the blueprint object. In your case, something like PlatformPre.parent=
. You can change the newly created gameObject all you want. Once it's spawned, it's the same as any other gameObject.
Follow your line Transform platformChild; platformChild = Instantiate(PlatformPre, New Vector3...) as Transform;
with platformChild.parent=
. You should be able to set platformChild.rotation
to whatever you like, as well.
Yeah, that's what I thought so too. But it either gives me a NullReferenceException at platformChild.parent = ;
Or it will still give me the Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
What you are currently doing is something very similar to this:
class Thing extends $$anonymous$$onoBehaviour {
public GameObject instantiatable;
public Transform parentToThis;
public void Start() {
... GameObject.Instantiate(instantiatable);
instantiatable.parent = parentToThis;
}
}
You need to do this:
public void Start() {
Transform temp = GameObject.Instantiate(instantiatable).transform;
temp.parent = parentToThis;
}
Umm.
Transform temp = Transform.Instantiate(instantiatable) as Transform;
Otherwise it will give you the "Can't convert GameObject to Transform" and "Are you missing a Cast?" Errors.
Just getting a small NullReferenceError when I try to respawn the platforms, gimme a $$anonymous$$ to fix it. Then check to see if that helped!
Okay Loius's method seems to have worked. Just need it to NOT spawn on top of each other.
Otherwise SWEET!
Transform temp = Transform.Instantiate(instantiatable) as Transform; <--- That version by the way.
OH and I also had to change EVERY BLOODY thing into Transform ins$$anonymous$$d of GameObject.
Also, the Instantiate had to be called inside the collision script otherwise you get a NullReferenceExcemption error.
AND I JUST REALIZED I A$$anonymous$$ THE BIGGEST IDIOT IN THE WORLD!!!!!!
So. I have this prefab... the one the platforms are parented to.. yeah... umm.. that is in fact a prefab.. and for some odd reason my brain did not read that as "PREFAB" and ins$$anonymous$$d read it as "DERP".
So problem solved!