• 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
1
Question by Joseph Lerner · Nov 27, 2014 at 08:24 PM · c#arraycolorspriterendererall

Finding all sprite renderers in the scene and tell them to change color

I'm working on a game where you need to cycle through different map modes. When you change the map mode, all my sprite renderers in every region change color. I cant have a script for every region doing this because I will have a ton of regions.

This is the script that I thought might work:

 using UnityEngine;
 using System.Collections;
 
 public class StateVariableAndMapmodeAffectsOnStates : MonoBehaviour {
 
     public GameObject[] stateDataArray;
 
     void Start () 
     {
         stateDataArray = GameObject.FindGameObjectsWithTag("Region");
     }
 
     void Update () 
     {
         foreach(GameObject go in stateDataArray)
         {
             GetComponent<SpriteRenderer>().color = Color.red;
 
         }
     }
 }
     
     

This doesn't do anything right now when I run the game. I need to make a script that finds all the sprite renderers in the scene and can constantly tell them to change color. If you know how I should change my script to do this, please tell me.

Also, should I be using an array here or a list? I will be having multiple variables factor in to how the sprite renderers change color, so I thought I should use an array.

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

Answer by huulong · Sep 23, 2016 at 03:57 PM

You have probably solved your problem in 2 years, but here is for the archive...

You are not using go in your Update method. It should be:

 go.GetComponent<SpriteRenderer>().color = Color.red;

You can also add more tests to check if the SpriteRenderer component exists, if some objects tagged "Region" do not have a SpriteRenderer. I don't know what your regions consist in exactly. Are they parents of the sprite objects? In this case, you should iterate on their children:

 foreach (GameObject region in stateDataArray)
 {
     foreach (GameObject child in region.transform)
     {
         // apply operation on child
     }
 }

Or maybe you have a few different region parent objects associated with a few different colors? (with tags Region1, Region2, etc.)

In this case, you can reference the regions to some script in the scene in the editor, so that you don't have to find them in runtime (that said, finding objects only on Start is fine).

If you have objects here and there and you want them to react to a specific signal, consider using C# events: Handling and Raising Events (Microsoft documentation) and C# Events in Unity

I guess you wrote Update() just for the example. Of course, you should change the color sprite just when you need it.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problem with a color randomization 2 Answers

How can I set the color of a sprite with the Sprite-Lit-Default material? 0 Answers

Make ALL gameObjects in an array do the same thing with one line of code 2 Answers

Array of colors not working, all sprites turn white. 1 Answer

Change color of all buttons listed in array 3 Answers

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