Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by NinjaSu · Nov 20, 2013 at 01:50 PM · c#instantiateprefabslistsdictionary

Dictionary Keys to Stack Enemy Types

 public abstract class Enemy : MonoBehaviour {}

Then

 public class A : Enemy {}
 
 public class B : Enemy {}

Then I want to make a key to represent instantiated objects of those two classes.

 public class Test : MonoBehaviour
 {
    // prefabs
    public GameObject a, b;

    public Dictionary<Enemy, Stack> eDictionary; 
 }

I do plan to store instantiated gameObjects into the dictionary.

How do I make the keys?

How do I store the gameObjects into the dictionary on their void Awake() functions?

The main problem is: How do I make the keys in such a way that the Enemies of type A stack together with 1 key

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
2
Best Answer

Answer by Azrapse · Nov 20, 2013 at 02:04 PM

It can be done in several ways.

If you really require every Enemy to register themselves into the dictionary on Awake, then the dictionary must be accesible from the Awake method. For example a singleton or making eDictionary public and static. I would go for the singleton, with specific methods for adding and removing Enemies to and from the dictionary. But your needs might vary.

This is a proposal using a static class.

 public static class EnemyStacks
 {
     static Dictionary<Type, Stack<Enemy>> eDictionary;
     static EnemyStacks()
     {
         eDictionary = new Dictionary<Type, Stack<Enemy>>();
     }
 
     public static void Push<T>(T enemy) where T: Enemy
     {
         if(!eDictionary.ContainsKey(enemy.GetType())
         {
             eDictionary.Add(enemy.GetType(), new Stack<T>());
         }
         eDictionary[enemy.GetType()].Push(enemy);
     }
 
     public static Enemy Pop<T>() where T: Enemy
     {
         return eDictionary[typeof(T)].Pop();
     }    
     
 }


Then in the respective enemy subclases

 void Awake()
 {
     EnemyStacks.Push(this);
 }
Comment
Add comment · Show 3 · 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 NinjaSu · Nov 20, 2013 at 02:10 PM 0
Share

The problem is trying to group all the Enemies of type A into one stack. So a key would give you access to a stack of all the A enemies.

Your Awake code will make a key for n enemies.

avatar image Azrapse · Nov 20, 2013 at 03:29 PM 0
Share

Okay, I misunderstood (although I would swear your question didn't state the same in the last lines...) Now it's doing as you want.

avatar image NinjaSu · Nov 24, 2013 at 11:26 AM 0
Share

That is a very good answer. I really like that you mentioned one of my favorite design patterns as well :)

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

18 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

Related Questions

Call a function in an instanciated prefab has no effect on the prefab 2 Answers

How can I create a tree like list/array structure? 0 Answers

Instantiated prefab doesn't collide with player 0 Answers

Creating Prefabs at runtime? 1 Answer

instantiate prefabs y+1 from last instintiated prefab 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