Hello guys, I have a little problem here.
I got a GameObject in my scene, which I would like, when clicking a button, to spawn 8 new object (sphere) around it, in each directions. I started by making one, since making the others would just need to change the Vector3 of the instantiate.
But it’s not working properly, it keep spawning without pause. I need it to spawn just one than stop.
I tried a the code found here Unity - Scripting API: Object.Instantiate
But it doesn’t work. So if anyone can help, thanks a lot in advance
using UnityEngine;
using System.Collections;
public class virustir : MonoBehaviour {
public GameObject prefab1; //my sphere to instantiate. It's just a sphere gameobject with no rigidbody, no tag, nothing.
public float distance;
// Use this for initialization
void Start () {
distance = 0.6f;
}
// Update is called once per frame
void Update () {
if (clictirvirus.shooting) { //check if the button is on
for (int i = 0; i < 1; i++) { //attempt to limit spawning to one
Instantiate( prefab1, transform.position + ( new Vector3(0,1,0) * distance), transform.rotation );
i++;}
}
Debug.Log ("Virus tir " + clictirvirus.shooting);
}
}
you can save them all them as prefab and do something like
public GameObject Something;
public Transform Spawn;
OnMouseDown(){
Instantiate(Something,Spawn.position,Spawn.rotation);
}
add this script to the yellow cube make spawn position just some gameObject where u want them to spawn and gameobject something your thing,player or anything u want to spawn.
for (int i = 0; i < 1; i++) { //attempt to limit spawning to one
Instantiate( prefab1, transform.position + ( new Vector3(0,1,0) * distance), transform.rotation );
i++;
break; // the for loop will start.
}
then you can continue to do whatever else you want to do!
public GameObject prefab1;
public GameObject rotator; // attach an empty object that is at the same position as the object with this script
public float distance;
public int amountOfObjectsToSpawn;
public Vector3 rotatorAxis;
// if conditions desired met
for (int i = 0; i < amountOfObjectsToSpawn; i ++)
{
GameObject instance = Instantiate (prefab1, transform.position + Vector3.up * distance, Quaternion.identity) as GameObject;
instance.transform.parent = rotator.transform;
rotator.transform.rotation += rotatorAxis * 360 / amountOfObjectsToSpawn;
}