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 /
avatar image
0
Question by e1d1w1a1r1d1 · Feb 05, 2018 at 05:14 PM · arrayprefabsmaterials

When creating a 2D array as a grid of cubes they are all the same colour,My array of gameObjects are all the same colour, even when I set them differently

I am trying to display a 2D array of 1s and 0s as cubes, which are either green or white. However, all of the cubes are the colour of the first one, so if grid[0,0] == 1 all of the cubes will be green. Here is my code:

  for (int i = 0; i < width; i++) // draws grid
          {
              for (int u = 0; u < height; u++)
              {
                  if(grid[i,u] == 1)
                  {
                      point = Instantiate(onePrefab);
                      Vector3 pos = new Vector3(i, u, 0);
  
                      point.localPosition = pos;
                  }
                  else if (grid[i,u] == 0)
                  {
                      point = Instantiate(zeroPrefab);
                      Vector3 pos = new Vector3(i, u, 0);
  
                      point.localPosition = pos;
                  }
           }
  }

onePrefab is a prefab with a green material

zeroPrefab is a prefab with a white material

Comment
Add comment · Show 1
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 shieldgenerator7 · Feb 05, 2018 at 05:44 PM 0
Share

Are they using the same material? $$anonymous$$aterials are assets, so you change it for one object and it automatically gets changed for all objects. It might explain why all your cubes are getting their color set at the same time

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ShadyProductions · Feb 05, 2018 at 05:26 PM

Are you very sure you set them up correctly in the inspector? Perhaps there are only 1 values in the grid, Try add a debug line in both if's and check if both are being called. Or take a look at the array object in debug mode

Comment
Add comment · 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
0

Answer by e1d1w1a1r1d1 · Feb 05, 2018 at 06:44 PM

It seems that the grid did only contain only 1s or 0s like you suggested, and changing System.Random.Next(0,2) to Unity.Random.Range0,2) solved the problem.

Thank you!

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 ShadyProductions · Feb 05, 2018 at 07:31 PM 0
Share

Using the .net variant of Random should work fine too aslong as you only create one instance

 public Random Random;
 
 private void Start() {
 Random = new Random();
 }

then you can call it like Random.Next(0,2) The issue you encountered is probably because you made a new instance of Random class every loop, which would give u probably the same result.

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

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

Related Questions

What happened to my Materials and Prefabs? 1 Answer

best practices for importing and updating models 0 Answers

gameobjects and prefabs losing materials 2 Answers

How to learn amount of objects in selected prefabs folder? 0 Answers

array with a list of materials 3 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