Object Pooling Spawn Script
Hi,
I am currently trying to create a game where there are random objects spawned depending on the player's score. I have created a spawn script based on the 'Infinite Runner' tutorial that uses several if/else statements to determine which array of objects should be spawned. (I tried a switch statement but wasn't quite able to get it to work -- I am a novice coder).
I've been trying to figure out a way to get the arrays into different object pools. I have watched the object pooling video live training but I didn't quite understand how it could apply to this situation where I am spawning different objects from different arrays.
Any information on where to start or how to better optimize this would be greatly appreciated! :-)
Code below:
public GameObject[] obj;
public GameObject[] obj2;
public GameObject[] obj3;
public float spawnMin = 1f;
public float spawnMax = 2f;
public int playerScore;
void Start() {
Spawn ();
}
void Spawn()
{
if(playerScore < 10) {
Instantiate(obj[Random.Range (0,obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
} else if(playerScore <20) {
Instantiate(obj2[Random.Range (0,obj2.GetLength(0))], transform.position, Quaternion.identity);
Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
} else if(playerScore <30) {
Instantiate(obj3[Random.Range (0,obj3.GetLength(0))], transform.position, Quaternion.identity);
Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
}
}
}
heads up, you're using "obj.GetLength(0)" for all three instantiation statements, when you should be using "obj.GetLength(0)", "obj2.GetLength(0)", and "obj3.GetLength(0)"
Doh! Thanks for the catch! That's what I get for copy-pasting. I appreciate it!
Answer by cjdev · Jan 05, 2016 at 02:55 AM
You're right to use if statements for the relative comparisons, switch statements can only be used to compare specific values. I don't see much wrong with your code although a potential problem if it's not by design is that you could spawn the same GameObject more than once. If that's not your intention you could get around it by keeping an array of the indices of your GameObject array that haven't been chosen and then use those as the random indices, taking out each as it's used. There's nothing really wrong with the structure of your code that I can see, normally I'd say boilerplate code is a prime target to get refactored but honestly with something this small it's not really necessarily and may even over-complicate it. Just to show you what it might look like for future reference in larger projects:
public void Spawn()
{
if (playerScore < 10) Spawner(obj);
else if (playerScore < 20) Spawner(obj2);
else if (playerScore < 30) Spawner(obj3);
}
public void Spawner(GameObject[] gos)
{
Instantiate(gos[Random.Range(0, gos.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
The idea is to encapsulate repetitive tasks for re-use but again, I don't think you have to worry about it in this case.
Thank you so much for the thoughtful answer! I am trying to $$anonymous$$ch myself coding from the ground up and trying to apply some of the concepts to real life applications is sometimes a bit daunting. I seem to understand the syntax very well but I am having some difficulties familiarizing myself with the API.
Your answer was very helpful in advising that my script will run fine and you provided a great example code that taught me a new concept!
Your answer
Follow this Question
Related Questions
can't save values from another class to a list 1 Answer
Picking up an item and spawning a new one. 0 Answers
End Game (Stop spawning) with OnTriggerEnter or OnCollisionEnter? 0 Answers
Unity 5 Sound repeating when detected by raycast JavaScript 2 Answers
Multiple script instances in a scene but need to access certain ones 0 Answers