- Home /
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.
Answer by merkaba48 · Jul 05, 2017 at 12:58 PM
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.
I'm sorry for not share the code.
public GameObject hazards;
public Vector3 randomPosition;
void Start ()
{
CallTheRandomRespawn ();
}
void CallTheRandomRespawn()
{
Vector3 position = new Vector3 (Random.Range(-randomPosition.x, randomPosition.x), randomPosition.z, randomPosition.z);
Quaternion rotation = Quaternion.identity;
Instantiate (hazards, position, rotation);
}
I'm not create prefab with DnD, I'm create with code. I set the position but it's create random position.
Edit: I found my mistake, what a dumb fault. I have written randomPosition.x ins$$anonymous$$d of randomPosition.y
Answer by Dyougi · Jul 05, 2017 at 04:02 PM
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.
Your answer
Follow this Question
Related Questions
Click Events in Instantiated prefab does not work 2 Answers
How to display gameobject(prefab folder) as image? 1 Answer
Instantiating a new clone of a prefab whilst setting the previous clone as inactive 0 Answers
Create a controller for a prefab that will live update all of the instances in the scene. 1 Answer