- Home /
Object Instantiate two times when i SetParent
Hi, I'm trying to instantiate a number X of gameObjects as children of a gameObject A (on which the script is attached). when i'm instantiating them not as children, everythings goes as planned.
But when i SetParent them, another gameObject gets instantiated as the child of the child of my gameObject A.
Here's the code where the magic happens. The fact that i use a coroutine change nothing the situation.
IEnumerator CreateSpineDeformation(float halfSizeY, float scaleCoefficient, int iterations)
{
GameObject deformation = Instantiate(gameObject) as GameObject;
deformation.transform.position = transform.position + Vector3.up * halfSizeY;
deformation.transform.localScale = transform.localScale / scaleCoefficient;
deformation.transform.RotateAround(transform.position, transform.up, UnityEngine.Random.Range(rotateUpCoef * -1, rotateUpCoef));
deformation.transform.RotateAround(transform.position, transform.right, UnityEngine.Random.Range(rotateRightCoef * -1, rotateRightCoef));
deformation.transform.RotateAround(transform.position, transform.forward, UnityEngine.Random.Range(rotateForwardCoef * -1, rotateForwardCoef));
deformation.transform.SetParent(transform); //HERE
DeformationManager deformationManager = deformation.GetComponent<DeformationManager>();
deformationManager.iterations = iterations;
deformationManager.scaleCoefficient = scaleCoefficient;
deformationManager.sources = 1;
yield return null;
}
Here is where i call it
void StartDeformation() {
for (int i = 0; i < sources && iterations > 0; i++) {
StartCoroutine(CreateSpineDeformation(source.GetComponent<Renderer>().bounds.size.y / spawnPositionRatio, scaleCoefficient, iterations - 1));
}
}
It's my first time uploading to this forum. Hopes i'm posting right. Thank you !
Answer by dorusoftware · Jan 12, 2021 at 03:32 PM
this looks a bit funny for me
for (int i = 0; i < sources && iterations > 0; i++)
maybe is something that can be done but I never saw this till now, I always checked if i is less or greater that a number in a for loop
i had a if statement after the loop, i wanted to shrink my code idk if this is the right way to do it but it works ! to my sense, the second part of the statement just take 0 or 1, so as long as you put something that can have those values its ok !
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
SetParent help. 0 Answers
Distribute terrain in zones 3 Answers
Controlling Parent-Child Objects via Script 0 Answers
Instantiate and arrays 2 Answers