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 T27M · Dec 11, 2012 at 03:13 PM · c#liststatic

Populating a list with multiple GameObjects

Hello

My gameobject's are subscribed to an event that when called, passes the gameobject to a static method which takes care of disabling and hiding that gameobject. The static method also adds that gameobject to a list so I can keep track of the disabled gameobjects.

My question is when I call this static method on multiple objects at the same time.

1) How is the order to which they are added to the list decided?

2) Is a list the best way to keep track of them?

I'm thinking about storing the transform of the gameobject before it is disabled, so when I come to enable it I can set it back to it's previous position.

I will add that I haven't use static methods or variables often, but I would like to be able to see the list of gameobjects in the inspector. Since the method is static I made the list static, from my understanding of static variables that would make sense as I don't need multiple instances of this class( correct? ), but static variables don't show up in the inspector.

3) Should I make them non-static and create an instance of the class at start time?

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

Answer by Philipp · Dec 11, 2012 at 05:57 PM

  1. How do you call the method "on multiple objects at the same time"? Is the method called by events that are fired by Unity for multiple objects "at the same time" (such as OnBecameVisible, if several objects appear within the same frame)? If so, the order in which objects are added to the list is the order in which the events are fired, which I'd consider to be undefined.

  2. Sure, a List is fine – unless you're dealing with vast amounts of objects, you're not going to find anything noticeably more efficient.

  3. Consider making a Singleton of sorts: Have an instance of the class attached to a GameObject, but make the instance register itself as a static variable. Have a static method that returns this variable, and use that (rather than Find()) to access your instance.

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 T27M · Dec 12, 2012 at 12:35 AM 0
Share

Thanks for your reply I'll have to look into the Singleton design pattern.

Currently I'm just testing the way I have things, at the moment an event calls a method in a script attached to each gameobject; The event is fired via a GUI button, but later on will be fired by collision or other methods. $$anonymous$$ost of the time in my game multiple objects won't be added to the list at the same time, but there may be times when that happens such as some sort of AOE weapon.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

Sort List by string field 1 Answer

C# NullReferenceException 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