- Home /
Changing the colour of all of the gameobjects in a list C#
I have a list of cities the player owns which changes regularly and I need to change the colour of all of the cities in the list to green. How do I go about this?
Answer by Hannibalov · Oct 31, 2012 at 12:54 PM
Find all gameObjects you need (there are various ways to do that), and then:
foreach(GameObject c in cities)
city.renderer.material.color = Color.green;
(Assuming cities is the list of cities you need to change the color to)
Thanks for the quick reply! I have another issue before I can see if it works, I've basically got a base city class which the other city scripts inherit from and in this i declare the list. I'm trying to add the cities to the list from the inheriting script, but it's not editing the list in the base class - each city is making its own list and the lists aren't interacting.
How do I edit the list in the base class from the inheriting classes?
Sorry, but can you rephrase that? When it comes to inheritance, it better be cristal clear, and I didn't get it 100%.
I have a base city class attached to an empty, and then different scripts which are attached to the cities themselves. The scripts on the cities inherit from the base city class.
Ok, then either you don't fully understand how inheritance works or I still didn't understand what's happening.
In case it's the first one, here's a quick explanation: Classes do inherit from other classes, but objects don't, they are different instances. So either you create a singleton class (a single instance) that holds the list of cities, or you make a method to find all cities on demand and you get rid of the list.
But bear in $$anonymous$$d that as far I understood, you are creating multiple instances of objects and you can't expect these objects to share information because their class inherits from the same base class. That's just not how it works.
I know they don't share information, I'm asking how to make them share information