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 dr_adrii · Aug 21, 2016 at 04:00 PM · 2dspawningsidescrollerdesign-patterns

Spawner design for variable spawned units

Hi,

So I already have a spawner class working fine that spawns a unit based on an instantiation of a single predefined prefab. Great.

Now what I'd like is a spawner that is able to spawn from a selection of unit options, similar to what you see in Pixel Academy or Storm of Vengeance. I'd assume the user would have to define a roster to take with them into the level. Likewise, the enemy spawner would have preselected units as options.

For this to work, what is the best way to reference at runtime the available prefabs? Do I need a class that initially stores a reference to all the default prefab unitd? It would pass the user selected options to the spawner class as and when required? Or is there a more standard way that my inexperience is missing, assuming different unit types are 50+?

If any has a link to anything that I could learn from, that'd be great as I'm not necessarily asking for a coded solution to be provided.

Thanks

Comment
Add comment
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

1 Reply

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

Answer by BongWater · Aug 21, 2016 at 08:34 PM

I belive what you're looking for is what's called an "Array" or "Object Array". Heres an example from one of my scripts I use to spawn coins on my 2d side-scroller project;

         // This is the Array game object. The [] creates an array
         public GameObject[] coins;
 
     private float coinSpawnTimer = 7.0f;
 
     void start () {
     }
 
     // Update is called once per frame
     void Update () {
         player = GameObject.FindGameObjectWithTag ("Player");
         
                 coinSpawnTimer -= Time.deltaTime;
         
         if(coinSpawnTimer < 0.01) {
             SpawnCoins();
         }
 void SpawnCoins() {
         Instantiate (coins [(Random.Range (0, coins.Length))], new Vector3 (player.transform.position.x + 30, Random.Range (0, 3), 0), Quaternion.identity);    
         coinSpawnTimer = Random.Range (2.0f, 4.0f);
 
 // The coins[(Random,Range (0, coins.Length))] means pick a random object between 0 (first object in array) and the arrays length.



When you assign the script to an object you'll get a drop down menu for the array, in my case Coins, then you drag and drop the prefabs into the menu, the first object being assined to "0". Hope this helps

Comment
Add comment · Show 2 · 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 dr_adrii · Aug 22, 2016 at 06:47 AM 0
Share

Hi, I wasn't aware you could do this, and I will give it a go. $$anonymous$$y concern was is this the best way to handle this problem and is it very scalable? What if you want to assign your prefabs at runtime rather than via the UI?

avatar image BongWater dr_adrii · Aug 22, 2016 at 01:08 PM 0
Share

Hi there, honestly I wouldn't know if this is the "best way" as it's the only way I know of. There could be ways to assign the values on runtime but again I wouldnt know, I havn't been using unity that long (just over a year) but hopefully this way works well enough for you

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

83 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 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

What is the best way to add Fog in a side scrolling game? 0 Answers

Help with spawning Prefabs that go towards a player 2 Answers

How do I create a randomly generated sidescroller that has an end? 0 Answers

On a side scroller, how do I make characters walk through each other and also be able to trigger a collider? 0 Answers

Dialogue & stop walking 1 Answer


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