- Home /
instatiates at a bad position
I have 10 prefabs that are instantiated by a cube with a the following script attached to it:
public GameObject[] obj;
public float spawn = 2f;
// Use this for initialization
void Start () {
Spawn ();
}
void Update(){
transform.Translate (Vector2.up * Time.deltaTime);
}
public void Spawn(){
Instantiate (obj [Random.Range(0, obj.GetLength(0))],transform.position, Quaternion.identity);
Invoke ("Spawn", spawn);
}
}
the problem is that some prefabs get instantiated at a bigger x, like to the side, some with a negative x which spawns them to the other side causing only part of them to be seen and other is out of bounds, how do i position them all on same x?
EDIT: all of the prefabs are instantiated at x = 0, but each prefab is to be centered has different x, such as 1, 0.5, -6.3...
Do they spawn together, within the same period of time?
no, over time, every spawn seconds, as you see in the code, spawn = 2, so each 2 second a prefab is spawned. the cube goes up more and more and more, endlessly, so it spawns prefabs on the y axis
Answer by ethestel · Apr 06, 2015 at 03:50 PM
Hello,
Instantiate method relies on position of its game objects transform, which you change by calling transfrom.Translate on every frame update.
You can assign a constant Vector3 to the newly Instantiated objects position
//this will spawn all objects at 0,0,0 world coordinates
Instantiate (obj [Random.Range(0, obj.GetLength(0))],new Vector3(0,0,0), Quaternion.identity);
my cube always goes up and up and up, i need it to spawn at the position the cube is, but i need the x to be 0, Vector3(0,0,0) just spawns all prefabs at same place.
The Vector3 variable you're passing to that method specifies newly instantiated objects position. You can change Vector3's parameters to spawn the object wherever you want to.
Instantiate (obj [Random.Range(0, obj.GetLength(0))],new Vector3(0,transform.position.y,transform.position.z), Quaternion.identity);
This will spawn that object on the same world coordinates with the game object this script is attached on.
It instantiates at 0, some prefabs need like -6.3 to be on x=0, how can i access the transform.position.x of the prefab, each prefab has an x that i specified in order for it to be in the center, like 1.1, 6.3, and so on, how to specify that the place spawned is Vector2(x of the prefab, transform.position.y)
It instantiates at 0, some prefabs need like -6.3 to be on x=0
I think you're talking about the difference between position and localPosition, but there's no way I can be certain about it. Can you supply your project files?
I was building the prefabs in a different scene so i dont interrupt the main scene, so some aren't really in the middle, so they will be in the middle it needs to be like x=-6.3
Your answer
Follow this Question
Related Questions
Why is Prefab declared as type Transform? 2 Answers
Don't allow prefab to spawn if prefab already exists at the spawn location. 1 Answer
How to randomly instantiate other prefabs parallel? 1 Answer
How do i Instantiate a prefab with specific assests included 3 Answers
Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers