Trouble using GetComponent to override all the materials in my object

Hello, I’m currently working on the Unity 3d Obstacle Course. I went a little bit ahead and used a previous model I created a while ago and coded it to spin. When I got to the step about using GetComponent to change the color of the obstacles the players hit. I ran into a problem. While all of the obstacles would all turn red when the player hit, the kitchen knife’s hilt was the only thing to turn red. I think this is because all of the obstacles only have one material while the kitchen knife has three. Is there anything I can do to make the entire knife turn red and override all three materials?

Screenshot 2021-05-23 003848 Screenshot 2021-05-23 003951 Screenshot 2021-05-23 004020 Screenshot 2021-05-23 004039

1 Like

There’s a way you can fix this, if your knife is using several materials instead of just one, you’ll have to modify your code slightly, I’ll give you the pseudo-code for you to try out for yourself, and also the answer.

  • Store all the materials in an array or list.
  • Loop through all of them and change their color.
  • The end result should be something like this.
    MultiColoredCube RedCube

The code:

Material[] materials = GetComponent<Renderer>().materials;

foreach (var material in materials)
{
     material.color = newColor;
}

Okay thanks, I’ll try it out!

I just have one last question. What exactly do you mean by arrays. I created the model in Blender. The inspector looks like this
Changing all the materials of an object INSPECTOR
Do you perhaps mean turning all the materials into one by using UV Editing in Blender to make it appear the same while only using technically using only one material?

No, that’s not what I meant but that’s an excellent solution, doing that will also solve your problem without having to modify your code, and it’s actually a far better solution because it helps you keep your code simply and it avoids certain issues.

I’ll explain what an array is, just for you to be clear. An array is a type of list that you can create in C#, it is written with the following syntax:

Material[] _myMaterials;
//The brackets indicate that it is an array, a type of list.

//You can even see how it works in the inspector. 
//if you are curious, try this out INSTEAD.

[SerializeField] Material[] _myMaterials = null;

//Then you'll be able to see the array in the inspector,
//You'll see that it is quite similar to the image you posted.


An array can be populated with a lot of variables of the same type, in this case, the type is Material. If you create an array and then populate it with all the materials of your model, then you can go through all of them and change their color, or you can even specify which material you want to change, really amazing stuff.

If you still don’t follow what I’m saying, worry not, you’ll see them and use them during the course quite a few times, enough for you to get used to them.

Oh, and welcome to the community! Hope you have a great time here! Sorry for not noticing that this was your first post when I previously replied to you.

Thank you for the explanation about the arrays. I must admit, I don’t understand a lot about what you are saying in regards to arrays, but I hope I will soon. I’ll still try to implement the arrays since the whole point of learning is to go outside your comfort zone to learn more. Thank you again for the quick response. You don’t know how much I appreciate this help, and thanks for the warm welcome!

1 Like

Hi @RedStreakG,

I’m glad that Yee was able to help you learn a bit more. Don’t worry if you don’t understand every single detail yet. :slight_smile:

One of my favourite sources of information on C# is DotNetPerls. Maybe you know that website. If not, please take a look at this article:

https://www.dotnetperls.com/array

Perhaps it will help you understand what arrays are. Don’t get confused by the type names and variable names. An array is a concept, which is not bound to specific names.


See also:

1 Like

Thank you for the link. I’ll be sure to look into it. I’m halfway through the arrays and I think I have a decent understanding of how they work

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms