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 UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc · Oct 30, 2016 at 07:52 PM · c#loopforeacharray of gameobjects

How can I check the color of GameObjects in an Array and print out their color?

Hi, I've been struggling to do this for a while. I found this snippet of code written in JS to check the color of GameObjects with a certain tag. I want to do the same but in C#. Here is that code from 2014.

  var counter : int;
  var specifiedAmount : int;
  
  function Update()
  {
      counter = 0;
  
      for(var go : GameObject in GameObject.FindGameObjectsWithTag("Tile"))
      {
          if(go.renderer.material.color == Color.blue)
          {
              counter++;
          }
      }
  
 }

However, I have found that the "go.renderer" line is no longer available in Unity 5. Is there any fix for this?

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class ChangeColor : MonoBehaviour {
     float timeLeft = 10.0f;
     int counter; 
     Renderer rend;
 
 
     // Use this for initialization
     void Start () {
         counter = 0;
 
         rend = GetComponent<Renderer> ();
 
     
 
     }
     
     // Update is called once per frame
     void Update () {
         
 
         timeLeft -= Time.deltaTime;
         if (timeLeft < 0) {
             Debug.Log ("You covered " + counter + " tiles in 10 seconds!");
         }
 
         foreach (GameObject go in ..................)  {
             
             counter++;
 
 
         }
 
 
     }
 
     void OnCollisionEnter(Collision col) {
         
         rend.material.color = Color.blue;
 
         }
     }
 
 

What would I add to make it do what I want it to do?

Thanks,

Comment
Add comment · Show 7
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 Ahndrakhul · Oct 30, 2016 at 08:30 PM 0
Share

I think you need to use GetComponent to do what you're trying to do.

An updated version of that JS code would look like this:

 foreach (GameObject go in GameObject.FindGameObjectsWithTag("Tile"))  
 {             
     if(go.GetComponent<Renderer>().material.color == Color.blue)
     {
             counter++;
     }
 }

avatar image UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc Ahndrakhul · Oct 30, 2016 at 08:53 PM 0
Share

That's right Can you help me with that?

avatar image Ahndrakhul UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc · Oct 30, 2016 at 09:12 PM 0
Share

If you want to keep things similar to how you have them, you will probably have to split your one class into two. The ChangeColor class would be attached to each of your game objects that is tagged "Tile" and would consist only of OnCollisionEnter like

  void OnCollisionEnter(Collision col) 
 {         
        gameObject.GetComponent<Renderer>().material.color = Color.blue;
  }

The other class would be responsible for the timer and the counting and would be attached to an empty game object in the scene. It would look something like

 public class GameControl : $$anonymous$$onoBehaviour
 {
     float timeLeft = 10.0f;
     int counter;
 
     void Start()
     {
         counter = 0;
     }
 
     void Update()
     {
         timeLeft -= Time.deltaTime;
 
         if(timeLeft <= 0)
         {
             foreach(GameObject go in GameObject.FindGameObjectsWithTag("Tile"))
             {
                 if (go.GetComponent<Renderer>().material.color == Color.blue)
                 {
                     counter++;
                 }
             }
 
             Debug.Log("You covered " + counter + " tiles in 10 seconds!");
         }
     }
 }

Show more comments
avatar image UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc Ahndrakhul · Oct 30, 2016 at 08:58 PM 0
Share

The idea of the game is to run over as many tiles as possible and turn as many of them as you can to blue in 10 seconds and then display how many tiles are blue.

0 Replies

· Add your reply
  • Sort: 

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

252 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 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 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

(c#) - For/Foreach loop skips indexes, jumping to different indexes at random?? 0 Answers

While loop freezes game for 3 seconds then unfreezes (not infinite loop!) c# 1 Answer

how to check all elements in array 2 Answers

Unity crashes when using while loop 0 Answers

how to instantiate an object each time there is a certain amount of increase in the y position of another object? 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