Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Varten · Jan 04, 2016 at 02:56 AM · scripting problemarrayrandomspawningobject pool

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));
     }
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adam-Halley-Prinable · Jan 04, 2016 at 01:40 PM 1
Share

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)"

avatar image Varten Adam-Halley-Prinable · Jan 04, 2016 at 02:24 PM 0
Share

Doh! Thanks for the catch! That's what I get for copy-pasting. I appreciate it!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Varten · Jan 05, 2016 at 03:07 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

45 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges