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 Mij · Sep 15, 2013 at 06:17 AM · c#gameobjectarray

C# Should I store array of GameObjects or the scripts attached to them?

I'm dynamically creating GameObjects and attaching my script MapTiles to each one. I don't anticipate having to access the GameObjects at all, but I do anticipate interacting with the scripts. Is it better to store references to the GameObjects or their scripts in an array?

I'm assuming it's better to put the GO's in the array, but I couldn't find anything on the subject via Google, so I'd love to hear specific reasons why or why not to do it this way.

If it's helpful, here's a code snippet:

 private void BuildLevel() {
     print("BuildLevel() starting...");
     print("Columns: "+levelDataColumns+", Rows: "+levelDataRows);
     
     int columns = levelDataColumns;
     int rows = levelDataRows;
     mapTileArray = new MapTile[columns, rows]; //assign size to array
     
     for (int i = 0; i < columns; i++) {
         for (int j = 0; j < rows; j++) {
             GameObject go = new GameObject();
             MapTile mapT = (MapTile) go.AddComponent(typeof(MapTile));
             mapTileArray[i,j] = mapT;
             
             Material mat = GetLevelDataMaterial(i,j);
             mapT.InitializeMapTile(i*10,0,j*-10,mat);
             
         }
     }
 }

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

Answer by vexe · Sep 15, 2013 at 06:24 AM

You answered yourself:

"I don't anticipate having to access the GameObjects at all, but I do anticipate interacting with the scripts"

But then:

"I'm assuming it's better to put the GO's in the array"

What is it that you think is 'better' to store the GOs?

Not saying that it's BAD to store the gameObjects, but why would you do that if you don't need to ever access the gameObject and all you need is the script component? - it doesn't make sense.

In fact, if you stored the GOs, what happens later when you want to access your MapTile script from all elements in the array? you would have iterate and for each member you do a GetComponent< MapTile >... Why?

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 Mij · Sep 15, 2013 at 06:28 AM 0
Share

$$anonymous$$ainly I was worried it was a gotcha scenario where not storing references to GameObjects would result in unexpected behavior. Thanks for the quick response!

avatar image vexe · Sep 15, 2013 at 06:29 AM 1
Share

If you ever needed your GO for some reason, you can always, at any time get a reference to the gameObject of your $$anonymous$$apTile component by just my$$anonymous$$apTile.gameObject

avatar image Mij · Sep 15, 2013 at 06:41 AM 0
Share

Oh, that's handy. Thank you again! This was super helpful!

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

16 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

Related Questions

Need help with an array. - C# 3 Answers

How to copy part of an array 2 Answers

Multiple Cars not working 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

Save Gameobject in Array 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