- Home /
child instantiation and scale problems
Hi, I'm having a problem with Instantiating a new game object, so my problem is that the game object I'm trying to instantiate wich is a child of another game object, doesn't spawn in the right position exept if I set the scale of the parent to the initale value wich is 1,1,1. I already have a solution wich is modifying the size of my Go in photoshop instead of changing their scale in unity but I think that is not very recomanded and I'm sure it will cost me a lot of time later. thanks in advance
do not set parent while instantiating. after you instantiate assign the parent. it wont change the scale then
actually that is not an option for me because I attend to apply my script to a lot of parents and the position where the childs will be instatiated at is relative to the parent's centers and not the global center so I want to benifit this advantage to optimate my game and more. this method will give me the posibility to spawn the childs in the positions I want using the same position coordinations all I have to do is to apply the same script to all parents.
Answer by tormentoarmagedoom · Feb 03, 2019 at 11:46 AM
Good day.
Reading your Q and your Comments..
You always have 2 options to instantiate a object that will give differents final scale. Find your best.
Instantiate the object without parent, and then change its parent. This way the object will mantain its initial global scale.
Instantiate the object directly as a child of another, will directly apply the scale of the parent as reference, Its global scale will be this Vector3 parentLocalScale*ChildLocalScale
In the other side, you have the final position. The final position is always the same if you takea s reference the parent, so depending on what method from above you chose, you will need to correct the position after Instantiate/ParentSet.
Doing something like this will give you control of final scale and final position. Object will be the same global scale its stored as prefab.
GameObject NewObject = Instantiate (Object, position, rotation);
NewObject.transform.parent=ParentObject;
NewObject.transform.localPosition = PositionInParentReference;
NewObject.transform.rotation = RotationInParentReference;
Doing something like this will make the prebaj automatik change globalscale in reference of parent scale in ordeerr to mantain its own localsclae (Object global scale will be Vector3 parentLocalScale*ChildLocalScale)
GameObject NewObject = Instantiate (Object, ParentObject);
NewObject.transform.localPosition = PositionInParentReference;
NewObject.transform.rotation = RotationInParentReference;
Bye!
Your answer
Follow this Question
Related Questions
How to make an object scale indefinately on key press? 3 Answers
Instantiating Cube Prefabs with scale 3 Answers
Get scale of instantiated prefab and resize 1 Answer
Instantiated prefab scale is wrong. 2 Answers
Instantiate prefabs scaled 1 Answer