- Home /
How set position of spawning clones
Please help, how I can set spawning position (now is: 0, 0, 0) for below inscribed code.
I walked through the tutorials but without result.
Everything what I trying causing crash of structure Xn x Yn.
Thank you very much
 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehaviour {
 
     public GameObject Prefab;
     public float timer = 0f;
     public float time_of_spawn = 0f;
     public int Xn;
     public int Yn;
 
 
 
     
     void Update() {
 
 
         timer += Time.deltaTime;
 
 
 
         if (timer > time_of_spawn) 
         {
         
     
 
             for (int x = 0; x < Xn; x++)
             {
                 for (int y = 0; y < Yn; y++)
                 {
                 
                 
                 Instantiate(Prefab, new Vector3 (x, y, 0f), Quaternion.identity);
 
 
 
             timer = 0f;
 
     
 
         
                 }
             }
     }
     }
 }
You've not set Xn or Yn to anything are you setting that in the inspector and if so what are you setting it to?
If I set Xn: 8 and Yn: 8 so in game I make 8x8 ( 64 x prefab )in regular square ( I don't want scattered ), and I need - How I can set position for this square ? I must create next script ?
O$$anonymous$$ so it's not just that you haven't set them. Are you getting an error message?
Above-mentioned code running without error. But I dont know how set position of this spawning. For example (0f, 6f, 0f) ins$$anonymous$$d of default (0f, 0f, 0f).
Answer by Mmmpies · Jan 03, 2015 at 07:22 PM
Well your code is almost there, I'm assuming this is a 3D game as you're using a vector 3 let me know if not.
I edited your code, the timer thing was just looping and creating lots of instances so I just put a bool in to spawn your 64 objects then stop.
 using UnityEngine;
 using System.Collections;
 
 public class Spawn: MonoBehaviour {
     
     public GameObject Prefab;
     public float timer = 0f;
     public float time_of_spawn = 0f;
     public int Xn;
     public int Yn;
 
     private bool spawn = true;
     
     void Update() {
         //timer += Time.deltaTime;
 
         if (spawn) 
         {
             spawn = false;
             for (int x = 0; x < Xn; x++)
             {
                 for (int y = 0; y < Yn; y++)
                 {
                     Instantiate(Prefab, new Vector3 (100 + x,  0.5f, 100 + y), Quaternion.identity);
                     //timer = 0f;
                 }
             }
         }
     }
 }
You also had y being used which in 3D is upwards, I didn't change the name just move it to the Z position.
I also instantiated (cubes in my test) so I set the Y value to .5f so it didn't drop through the terrain.
Finally I added 100 to X and Z so it was away from the edge of the map but should still work for you if you want them to start from 0 0 0.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                