Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by JanZagar · Dec 12, 2015 at 05:00 PM · c#gameobjecteditorarraynoob

Can i chose game objects as parts of an array in a script ?

Hey guys i need some help...

I created a 2D array that is 10x10, Now i have to fill those arrays with game objects of a grid that is 10x10. Grid is create with 100 cells that each use this script. Now i don't have any easy way to put all this cell in arrays with a script, because the only way (that i know) is to create 100 public gameObject variables. That means i would have to chose each cell for a variable manually with editor. And do that 100 times for each cell with this script. That's why im looking for a solution within a script. If you know that solution please anwser below. And if you could please don't be mean, im new to Unity.

Thank you in advance.

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

Answer by dhore · Dec 13, 2015 at 02:40 AM

The easiest way that I can think of is if you make an empty gameobject and then add the 100 cell objects all as children to the empty object, then add the script to the empty object and use a loop like this:

 GameObject[,] cells = new GameObject[10,10]; // the array
 
 void Start()
 {
     // not sure what function you want it in, so I'll just put it here
     
     for (int i = 0; i < transform.childCount; i++) // loops through all the children
     {
         cells[i/10, i%10] = transform.GetChild(i).gameObject;
     }
 }
Comment
Add comment · Show 2 · 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 JanZagar · Dec 13, 2015 at 07:44 PM 0
Share

Thx this works, but i have one more question. Im trying to make it so that each object/cell in the array will go true a switch loop. That swith loop consists of 4 cases, depending on which case it is it would change color. Do you have any advice ? Thx

avatar image dhore JanZagar · Dec 14, 2015 at 01:12 AM 1
Share

You'd be looking for something like this:

 for (int i = 0; i < cells.GetLength(0); i++)
 {
     for (int j = 0; j < cells.GetLength(1); j++)
     {
         // check cases here
         // if it matches the case then:
         cells[i,j].GetComponent<Renderer>().material = red$$anonymous$$aterial;
     }
 }

But ins$$anonymous$$d of "red$$anonymous$$aterial" you'd use whatever your material name is for that case. Just remember that you'll need to make a new material for every different colour - and then I'd suggest giving them to the script via public variables.

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

43 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting collision for individual gameobjects in an array and changing properties of other gameobjects from that array 0 Answers

Access GameObject Array and Turn Render on via the key pressed - Keys are already named based on game object. 0 Answers

[HELP C#] Making Gameobject 1 equal Prefab 2 1 Answer

Getting collision from gameobjects in an array, and changing properties of other gameobjects from that array 0 Answers

Getting Collision from an array and changing properties from other gameobjects in that array 0 Answers


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