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 Sonderangebot · Aug 29, 2015 at 01:38 PM · c#2dobject pool

Can I change which GameObject is being drawn from a pool via a public variable/the property inspector?

Hey all,

I'm moving away from using instantiations for GameObjects, but there's one thing that I've been puzzling over the last couple of days.

I'm developing a shoot 'em up in 2D, and currently trying to use pools to store bullets and enemy defeat animations (like explosions and death animations). It's possible to attach different types of pods to the sides of your character that fire bullets drawn from different pools. Currently, the main character and the pods use the same script for firing.

To differentiate between the type of bullet each pod fires, I've use a public float and changed the value in the property inspector for each prefab. Currently my firing script contains this (each pod fires two different bullets):

 if (shotType == 1)
 {
     shotTransform = ObjectPool.mainShotInst1.GetmainShotObj1();
     shotTransform2 = ObjectPool.mainShotInst2.GetmainShotObj2();
 }
 if (shotType == 2)
 {
     shotTransform = ObjectPool.sideShotInst1.GetsideShotObj1();
     shotTransform2 = ObjectPool.sideShotInst2.GetsideShotObj2();
 }
 if (shotType == 3)
 {
     shotTransform = ObjectPool.sideShotInst1.GetlaserObj1();
     shotTransform2 = ObjectPool.sideShotInst2.GetlaserObj2();
 }
 if (shotType == 4)
 {
     shotTransform = ObjectPool.sideShotInst1.GetmultiObj1();
     shotTransform2 = ObjectPool.sideShotInst2.GetmultiObj2();
 }
 shotTransform.transform.position = new Vector2(transform.position.x + offset.x, transform.position.y + offset.y);
 shotTransform.SetActive (true);
 shotTransform2.transform.position = new Vector2(transform.position.x + offset2.x, transform.position.y + offset2.y);
 shotTransform2.SetActive (true);

While this works, my game is going to have way more pods, meaning that the shooting script is going to get very complicated very quick.

Similarly, enemies use a HP script which draws a 'death' GameObject from the pool when their health reaches zero - something like an explosion or in most cases a unique death animation. However, since I'm going to have a ton of enemies, I don't know if the method I use above will be viable, since the script will probably run through about 50 'if' statements whenever the enemy dies (one statement for each type of enemy!).

This was all simple enough when I instantiated a public GameObject, since all I had to do select the desired object in the property inspector for each prefab, but I can't find a way to do anything similar for an Object Pool.

Is there any way to be able to change which objects are being drawn out of the pool using a public variable? The type of object to be drawn needs to be changed on the fly as well. Or is the best solution to hard code each script for each different enemy and pod? Ideally I would like the game to run on mobile too so I want to pick ideally the fastest solution.

My coding experience is pretty limited and this is my first post here, so please let me know if my question is too vague (since admittedly I'm at a loss to describe it) or if it would be better suited to a forum post. 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

0 Replies

· Add your reply
  • Sort: 

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

27 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

Related Questions

Move GameObject to Position, Waiting, and then Moving to Another Position 5 Answers

Unity jump from one canvas scene to another scene with canvas 2 Answers

C# Script - Erro CS8025: Parsing error 1 Answer

Disable load new level until all enemies are dead 2 Answers

Player follows mouse, but I need only Y 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