- Home /
Objects spawning in each other
Hello so i posted up this code the other day trying to figure out the problem i was having with it, it works for the most part perfectly but i'm having some issue with resources spawning within one another.
This problem happens because spawning is always on, as you'll see in the code i'm using a timer to decide when to spawn objects.
I have a fix in mind by splitting the objects up and giving them a separete spawn script with separete points to spawn at.
But i figured they're probably a 100 better ways -i don't know of- of doing that and keeping my code pretty much as it is. I'd like the code to able to check if a spawn point as been used and if so use a different point and if all the points are used stop spawning, i think of the top of my head i could change the spawn array into a list?-maybe-. but i'm unsure i'm still a unity noob and unsure on how to take this forward.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnTest : MonoBehaviour {
public float timer = 0.0f;
public bool spawning = false;
public GameObject[] resources;
public Transform[] spawn;
private GameObject resourcePrefab;
void Update (){
//check if spawning at the moment, if not add to timer
if(!spawning)
{
timer += Time.deltaTime;
}
//when timer reaches 2 seconds, call Spawn function
if(timer >= 2)
{
Debug.Log ("SpawnHit");
StartCoroutine(Spawn());
}
}
public IEnumerator Spawn (){
//set spawning to true, to stop timer counting in the Update function
spawning = true;
//reset the timer to 0 so process can start over
timer = 0;
Debug.Log ("Timer reset");
//select a random spawn point
int randomPick = Random.Range(0,spawn.Length);
//select a random object from array
int Clone = Random.Range(0,resources.Length);
//create the object at point of the location variable
Instantiate(resources[Clone], spawn[randomPick].position, transform.rotation);
//halt script for 1 second before returning to the start of the process
yield return new WaitForSeconds(1);
//set spawning back to false so timer may start again
spawning = false;
}
}
Answer by taylank · Mar 10, 2014 at 04:28 AM
i think of the top of my head i could change the spawn array into a list?-maybe-
Make that definitely. After declaring the list and populating it either in the editor or by code, you can do this:
//declaring the list here
public List<Transform> spawn = new List<Transform>();
.
.
.
if (spawn.Count != 0) {
Instantiate(resources[Clone], spawn[randomPick].position, transform.rotation);
// remove the point you just used
spawn.RemoveAt(randomPick);
}
else {
//if all points are used, set spawning to false
spawning = false;
}
Hello taylank thanks for the reply, it kinda works although the spawn points are being removed after each spawn. I don't know if i'm handling the list incorrectly or the removing part incorrectly.
Is there a way of using a spawn point 2 times? so it doesn't spawn an item twice, but if the player picks up an item at the point the spawner has a chance of spawning at the spawn point again.
Code so far
public class SpawnTest : $$anonymous$$onoBehaviour {
public float timer = 0.0f;
public bool spawning = false;
public GameObject[] resources;
public List<Transform> spawn = new List<Transform>();
void Start(){
}
void Update (){
//check if spawning at the moment, if not add to timer
if(!spawning)
{
timer += Time.deltaTime;
}
//when timer reaches 2 seconds, call Spawn function
if(timer >= 5)
{
StartCoroutine(Spawn ());
}
}
public IEnumerator Spawn (){
//set spawning to true, to stop timer counting in the Update function
spawning = true;
//reset the timer to 0 so process can start over
timer = 0;
Debug.Log ("Timer reset");
//select a random spawn point
int randomPick = Random.Range(1, 6);
//select a random object from array
int Clone = Random.Range(1,2);
//added code
if (spawn.Count !=0)
{
//create the object at point of the location variable
Instantiate(resources[Clone], spawn[randomPick].position, transform.rotation);
spawn.RemoveAt(randomPick);
}
else
{
//set spawning back to false so timer may start again
spawning = false;
}
yield return new WaitForSeconds (1);
spawning = false;
}
}