- Home /
Object instantiating on transform with offset, but there's an error that randomly changes position. SOLVED!
I have this code, which randomly instantiates prefabs, then deletes the transforms of where it connected on the object itself and the one it spawned on, this does work, correctly, but not always, sometimes there's random offset and I have no idea why, my code is messy but seems right??? Everything does get instantiated on a canvas as a child.
Make sure that you set all anchored positions in your prefabs right! (Or none at all)
while(DefinedInt == 0)
{
RandomInt = Random.Range(1, 5);
if(RandomInt == 1)
{
if(upOpenings.Count != 0)
{
chosenTransform = upOpenings[Random.Range(0, upOpenings.Count)];
var offset = new Vector3(0, 0, 0);
GameObject pipe = Instantiate(HasBottom[Random.Range(0, HasBottom.Length)], chosenTransform.position, Quaternion.identity, GameObject.FindGameObjectWithTag("Canvas").transform);
pipe.transform.position = chosenTransform.position + offset;
for(int i = 0 ; i < pipe.transform.childCount ; ++i)
PipesOpenings.Add(pipe.transform.GetChild(i));
Destroy(chosenTransform.gameObject);
foreach(Transform child in pipe.transform)
{
if(child.tag == "Down")
Destroy(child.gameObject);
}
DefinedInt++;
}
}
else
this repeats 4 times because of 4 positions, I've been trying to solve this problem for a week now! Thanks in advance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613044100im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to drag an instantiated UI element? 0 Answers
My variables arent updating accordingly after I instantiated them 1 Answer
How create ONLY a object ?! 2 Answers
Instanciate prefab responsively 1 Answer