- Home /
Number of spawned objects
Hello,
I have spawner script that is working as I wanted. Currently this scripts spawns a lot of objects, but I want only few of them. Is there any fix for my script? This is what I have:
using UnityEngine;
using System.Collections;
public class Spawner3 : MonoBehaviour
{
public float spawnTime = 5f; // Vrijeme između svakog "spawn-a"
public float spawnDelay = 3f; // Potrebno vrijeme da se objekt "spawn-a"
public GameObject[] kikiti; // Array
int enemyIndex;
public Mesh objectToCreate;
void Start ()
{
// Pozivanje Spawn funkcije odmah nakon što prođe potrebno vrijeme
InvokeRepeating("Spawn", spawnDelay, spawnTime);
}
void Spawn ()
{
var go = Instantiate(kikiti[enemyIndex], transform.position, transform.rotation) as GameObject;
//gameObject.AddComponent ("Rigidbody");
//go.rigidbody.AddForce(Vector3.up * 500);
var gameObject2 = new GameObject("kikiti2");
var meshFilter = gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
gameObject.AddComponent<Animator> ();
gameObject.AddComponent<Animation>();
meshFilter.sharedMesh = objectToCreate;
/*gameObject2.transform.position = transform.position;
gameObject2.transform.rotation = transform.rotation;
gameObject2.AddComponent<Rigidbody>(); */
}
}
Comment
Answer by Dblfstr · Mar 07, 2014 at 04:38 PM
Call it once from start()
void Start(){
Spawn();
}
And in your Spawn(), create a loop that will loop as many times as you want.
Void Spawn()
{
for(int i = 0, i<=howManyIwantToSpawn), i++)
{
//All your spawncode
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613135630im_/https://answers.unity.com/themes/thub/images/avi.jpg)