Although prefabs has position, creating random position.

Hello, everyone I’ve issue about creating prefabs. Yesterday I asked same question but noone answer it. But I found something. I tryed when game running I look at the “scene”. After I saw the prefabs creating random position. Let me show the photo.

this is what I will create

and it’s game scene when prefab created. Why it happend? How can I solve?

Thanks in advance.

How are you creating the prefab? If you’re dragging and dropping into the scene view, it will place it ‘somewhere’ in view, not at the origin. If you drag/drop it into the hierarchy view, it will instantiate at the prefab’s saved coordinates.

Vector3 position = new Vector3 (Random.Range(-randomPosition.x, randomPosition.x), randomPosition.z, randomPosition.z);
Here the first argument of your new Vector3 (which is x) is set with Random.Range which is returning a random number depending of the range you specified.
Instead of using this variable to set another one (Vector3 position), just use it when you instanciate your prefab

Instantiate (hazards, randomPosition, rotation);

Since randomPosition is public, you can assign the position you want in the Inspector.