- Home /
Spawn items around GameObject
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 http://docs.unity3d.com/ScriptReference/Object.Instantiate.html 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);
}
}
Where you want to spawn item's and how can u post picture :D
I want this (just dragged prefabs around to make that figure, but I want it to spawn. The yellow cube is my main gameObject and the green sphere are the items I'm trying to instantiate
The script would be attached to the yellow cube.
Answer by awplays49 · May 30, 2015 at 09:30 PM
try something like this
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;
}
let me know if all goes well :)
Answer by Johnz1234 · May 30, 2015 at 09:08 PM
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.
Answer by Eiyon · May 31, 2015 at 08:24 AM
if you want to stop your for loop just use break;
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!
Please read the FAQ and view the tutorial video to learn how to format script code.
Your answer
Follow this Question
Related Questions
Problem with spawn object 0 Answers
How to spawn gameobject from host to all clients 1 Answer
Spawning GameObject contained within another 1 Answer
object not spawning but no error message 2 Answers
How can I set a GameObject variable on an instantiated object? (It always instantiates empty!) 1 Answer