Hello so i’ve been on and off working on a spawning script for my game, it’s changed alot to suit the needs of the game and how it’s going to play. I’ve made up a code but for what it does i feel that there are many more efficient possibilities. Basically is there a better way of doing what i’m doing? -less code-.
At the moment(as shown below) i’m spawning 3 different objects at the start of each game, it then goes to the respective spawn method creates a list of points for the object to spawn at, picks a random point, spawns the object and then removes that point.(which is a bit of code i left on from my last script thought, basically it stops a object from spawning at the same place if i spawn 2 objects at 2 different points).
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnTest : MonoBehaviour {
public GameObject Copper;
public Transform[] copperSpawns;
public GameObject wood;
public Transform[] woodSpawns;
public GameObject Sand;
public Transform[] sandSpawn;
public int maxCopper = 0;
public int maxWood = 0;
public int maxSand = 0;
void Start(){
}
void Update (){
if(GameObject.Find("Copper(Clone)") == false)
{
spawnMetal();
}
if(GameObject.Find("Wood(Clone)") == false)
{
spawnWood ();
}
if(GameObject.Find ("Sand(Clone)" == false)
{
spawnSand();
}
}
void spawnMetal (){
List<Transform> spawn = new List<Transform>(copperSpawns);
for( int copperCount = 0; copperCount < maxCopper; copperCount++){
//select a random spawn point
int randomPick = Random.Range(0,spawn.Count);
Transform pos = spawn[randomPick];
spawn.RemoveAt(randomPick);
//create the object at point of the location variable
Instantiate(Copper, pos.position, transform.rotation);
}
}
void spawnWood (){
List<Transform> spawn = new List<Transform>(woodSpawns);
for( int woodCount = 0; woodCount < maxWood; woodCount++){
//select a random spawn point
int randomPick = Random.Range(0,spawn.Count);
Transform pos = spawn[randomPick];
spawn.RemoveAt(randomPick);
//create the object at point of the location variable
Instantiate(wood, pos.position, transform.rotation);
}
}
void spawnSood (){
List<Transform> spawn = new List<Transform>(woodSpawns);
for( int sandCount = 0; sandCount < maxSand; sandCount++){
//select a random spawn point
int randomPick = Random.Range(0,spawn.Count);
Transform pos = spawn[randomPick];
spawn.RemoveAt(randomPick);
//create the object at point of the location variable
Instantiate(Sand, pos.position, transform.rotation);
}
}
}
You are on the right path but still doing some stuff wrong. You are constantly creating a new list. No use. public GameObject Copper; public List<GameObject> itemCopper = new List<GameObject>(); public Transform[] copperSpawns; void Update(){ if(itemCopper.Count == 0)Invoke("spawnMetal",1); } Your spawnMetal method looks fine to me.
– fafaseI think you should redefine your principle, try this below if it works: void Start(){StartCoroutine(CreateMetal());} IEnumerator CreateMetal(){ while(true){ if(listMetal.Count == 0 && listMetal <= 10) { waiting = true; float t=0; while(t < 1f){ t+=Time.deltaTime; yield return null; } Instantiate();// with parameters } yield return null; } }
– fafase