• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

How to find all GameObjects in Hierarchy by name and put them in an Array[] ? (C#) 1 Answer

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

Array index out of range. But it doesn't appear to be. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges