So lets say I got 2 cubes
Cube1 and Cube2
I wan't to assign Cube1 as the Parent of Cube2, can anyone give a code example of how that is done?
So lets say I got 2 cubes
Cube1 and Cube2
I wan't to assign Cube1 as the Parent of Cube2, can anyone give a code example of how that is done?
Sure,
Cube2.transform.parent=Cube1.transform
I tried looping through and adding all my asteroid game objects to a parent game object and they all appeared on top of each other. Not sure why this is happening tho.
– Sentiencehave you tried to set the cube2 transform to a null first then add parent.
– BonucciAwesome.. my big problem was solved by your code :)
– VirguleAs this answer can be found on google: There is a SetParent method now. It keeps the local orientation. [ScriptReference/Transform.SetParent.][1] [1]: https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
– GonickI get the error "CS0103: The name `Cube1' does not exist in the current context" How do I find Cube1?
– Daubparent = gameobject
keeps the worldposition
of the child while SetParent
allows you the choice between world position or changing the position to be relative to the parent.
'cube2.transform.SetParent(cube1.transform, true);
'cube2.transform.SetParent(cube1.transform, false);
'cube2.transform.parent = cube1.transform;
You can see a demonstration of how these will all parent the object but have different results on the position here https://www.monkeykidgc.com/2020/12/tips-and-tricks-unity-set-parent.html
© 2022 - 2024 — McMap. All rights reserved.
But I think a missing audioSource gives a run-time error. And PlayOneShot is funny -- it's for "other" clips not attached to the audioSource. I use the OPs set-up (even 0.9 for sound level, empty clip in the audioSource slot) and it's fine. Now, if boom8 was empty, that won't give an error (you'd think it would, but I guess audio is used to getting nulls?)
– Maiden