I have been swinging at this for almost an hour now. I am giving up and asking for some help before I lose my sanity.
Here is the basis for what I have; I have tried all types of things from using arrays for the children to trying to use MeshRenderer instead of Renderer. I have had no luck, all of the children stay their own color instead of following the color the parent commands them to.
{
public GameObject ThisGameObject;
private Material TheColor;
public Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
TheColor = GetComponent<Renderer>().material;
foreach (Renderer child in transform)
{
GetComponent<Renderer>().material = TheColor;
}
}
}
Please help me deal with these unruly children and get them in line! I could edit them all manually, but I’d have to do it each time I want to change the color of all children of an object. A script should save me a lot of time, if I can get it to work!
How often is your loop actually called? You could - instead of using for each - create a list by using GetComponentsInChildren and check the count of what is returned.
General advice: If you want all the renderers (MeshRenderer is correct, btw) to have the same material, use sharedMaterial to reduce DrawCalls.
For one, I don’t think line 9 can iterate with anything other than a Transform type.
For two, line 11 will keep getting the same Renderer again and again, the same one accessed on line 8.
Line 11 should use the version of GetComponent that accepts an object, such as:
child.GetComponent<Render>() .... etc.
For three, you can use GetComponentsInChildren() to get all Renderers. BE SURE YOU READ ThE DOCUMENTATION for how it returns the object you start from too, which may be relevant in your case.
Finally, why is line 4 public? Seems only to confuse the intent here.
Either way, this can help you untangle any code:
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: