2
votes

I have a random world with some trees that get randomly instantiated.

I use the following code to instantiate them:

GameObject go = Instantiate(treePrefab);
go.name = "Tree";
go.tag = "Wood";
go.layer = 9;
go.transform.position = new Vector3(y + UnityEngine.Random.Range(-0.5f, 0.5f), heightPos, x + UnityEngine.Random.Range(-0.5f, 0.5f));
go.transform.Rotate(0, UnityEngine.Random.Range(0, 360), 0);

x and y are part of a for loop that goes through all positions (like a grid) of my world.

heightPos is the current height of the terrain at the specific point (x/y).

My problem is that the prefab ob the trees has the position 0, 0, 0.

Every tree that get's instantiated gets set to 0, 0, 0 too.

I use the exact same code to instantiate rocks, cactus, feathers, car's and many other things, and it works perfect.

but it always fails at the trees.

I've looked through the code and there is no difference to the other objects, except the prefab (of course).

So i think it has to do something with the settings of the prefab.

But my prefab got no animator, it is not static or something.

This is what my tree-Prefab looks like in the inspector:

enter image description here

and this is what it looks like in scene view:

enter image description here

1
Could you provide some some info on where you call the code you showed?Rodrigo Rodrigues
@RodrigoRodrigues i just added the whole code. It is inside the Start()-Method of a empty GameObject that is just for creating the world.AlpakaJoe
When i instantiate it using this code: GameObject.Instantiate(treeUF1, new Vector3(y + UnityEngine.Random.Range(-0.5f, 0.5f), heightPos, x + UnityEngine.Random.Range(-0.5f, 0.5f)), Quaternion.identity) it works, but i can't change the position afterwards.AlpakaJoe
...hmmm... I didn't mean to make you to post the whole class code. You should provide a minimal reproducible example that any of us can test on our editor and reproduce the wrong behavior.Rodrigo Rodrigues
Nothing else different about this prefab? Its not parented differently? No other scripts attached? Does it make a difference if you use transform.localPosition instead of transform.position to move it?Fredrik Widerberg

1 Answers

0
votes

You can create another gameobject and make this instance as a child of that gameobject so even if the instance is always at (0, 0, 0) you can always change the position of the parent object.