Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 tagovan · Jul 09, 2015 at 08:54 AM · gameobjectstaticcounter

A common way for counting instances of gameobjects that implement subclasses derived from a single base class

I am creating several gameobjects from prefabs. All of these gameobjects are assigned scripts which implement subclasses that all derive from a single base class.

I would like to be able to count how many types of certain gameobjects there are, as defined by which subclasses they have assigned to them. I want to be able to process all the gameobjects that derive from the base class in the same way.

Ideally would like to create a variable in the base class that is static in the subclasses, so I can process all the gameobjects in the same way, but have the variable different for each of the subclasses. However I can't seem to use abstract variables which are then later declared as static.

Anyone have any suggestions for this?

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

Answer by Nischo · Jul 09, 2015 at 09:40 AM

Something like this comes to my mind. But be careful with serialization and Dictionaries.

 class BaseClass : MonoBehaviour
 {
     void Start()
     {
     }
     
     protected static void Register(BaseClass aThis)
     {
         List<BaseClass> list;
         if(sInstances.TryGetValue(aThis.GetType(), out list)
         {
             if(!list.Contains(aThis) list.Add(aThis);
         }
         else
         {
             list = new List<BaseClass>();
             list.Add(aThis);
             sInstances[aThis.GetType()] = list;
         }
     }
     
     private static Dictionary<Type, List<BaseClass>> sInstances = new Dictionary<Type, List<BaseClass>>();
 }

If your classes now derive from BaseClass you just need to call Register(this) in the scripts Awake() method. In Instances you now have, sorted by type, a collection of your scripts, using the gameObject property on these scripts you can access the objects in your scene.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Static Facing an Object 2 Answers

Strange increment counter 1 Answer

Why my capsule collider sometimes stucks in other colliders? 0 Answers

Should non-rendered (code only) GameObjects be marked as static 2 Answers

When is a static game object static? 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