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 MCHammond · Nov 09, 2013 at 10:59 PM · gameobjectlistgetcomponentarrays

GetComponent for gameobject list alternative

My current setup is that I have 2000+ empty gameobjects in a scene all called "Hex" with a tag "Hex" and with a script attached called "Hex" that stores all the relative information about each hex.

like:

  • movementCost

  • adjacentHexs

  • connectedHexs "for linked non-adjacent hexs"

  • weightValue[] "for each player"

  • ect.. ect..

the problem is that I am using code like this alot, over and over again to do different things like move players, enemies or alter the map:

 foreach (GameObject hex in GameObject.FindGameObjectsWithTag("Hex"))
 {
     int Weight = hex.GetComponent<Hex>().weight;
     //do something
     int movementCost = hex.GetComponent<Hex>().movementCost;
     //do something
     GameObject[] adjacentHexs = hex.GetComponent<Hex>().adjacentHexs;
     //do something
     foreach (GameObject adjacentHex in adjacentHexs)
     {
             int adjacentWeight = adjacentHex.GetComponent<Hex>().weight;
             //do something
             int adjacentMovementCost = adjacentHex.GetComponent<Hex>().movementCost;
     }
      //do something
 }

I am using the GetComponent over and over again all over my code and its getting ridiculous. I need a better way of working that will aloow me to store all the data I need about all the Hexs in one place so that I can access it without the repeated GetComponent.

I was looking into creating a Hex class and then creating a list but Im not sure how to implement it or how to make it work without the GetComponent slowing everything down.

Any ideas?

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 Hoeloe · Nov 09, 2013 at 11:17 PM

Keep a static list of Hex objects in your class. In the Start method in your hex class, add it to this list. When you destroy a Hex object, remove it from that list. You now have a list of Hex objects that do not need GetComponent calls.

As another point, to get rid of multiple calls to GetComponent, just create a temporary variable, like this:

 Hex h = hex.GetComponent<Hex>();

And use that instead of calling GetComponent every time. That's not your only problem, though, as FindGameObjectsWithTag can be a pretty expensive operation too, so you don't want to be doing it every frame. Better to store the list somewhere and update it manually when it needs to change, rather than recreating it every frame.

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 MCHammond · Nov 09, 2013 at 11:42 PM 0
Share

Thank you, a "static list" is what I needed.

I should be able to remove the 2000+ instances of the Hex script from all the empties and get rid of all the GetComponent 's now :D

Here is the game that I am working on, as you can see its slow as hell atm and needs alot of recoding: https://dl.dropboxusercontent.com/u/38908748/$$anonymous$$ill_Dragons_for_Science/GamePlay_Prototype_V0.02/GamePlay_Prototype_v0.02.html

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

17 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

Related Questions

odd error in c# script when working with arrays 2 Answers

Get_GameObject non fatal error reoccuring 1 Answer

Getting errors when referencing a variable. 1 Answer

Storing and Removing GameObjects - List or Dictionary? 2 Answers

How to create an array of Transforms 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