- Home /
How to find all gameobjects that use the same material and change it?
Hi! Im a noob at programming, and im having some trouble with this simple task...
What i wanted is to find all gameobjects that use a specific material, and change it to another different material.
So in the script ive done, i have to put by hand the material i want to find and the material i want it to change to, and then in play time when i press space it was supposed to change to the new material but its not happening.
Could someone help me?
Thank you so much!!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Script_ChangeMaterials: MonoBehaviour
{
public Material materialtochange;
public Material materialchanged;
private Renderer rend;
private Material[] mats;
void Update()
{
if (Input.GetKeyDown("space"))
{
Renderer[] rend = FindObjectsOfType(typeof(Renderer)) as Renderer[];
for( int i = 0 ; i < rend.Length ; i++)
{
if(rend[i].material == materialtochange)
{
rend[i].material = materialchanged;
}
}
}
}
}
its a bit wierd. I went through this. you have to create a variable for the material in your script. then drag the shared material into the inspector. you manually change the texture and settings of the material in use to get your result. Changing which material the variable is holding wont do anything for you. and if you try to get a metrial from an object during runtime, it just makes an new material instance and won't change any other objects. so dragging and dropping in the inspector is the only way to edit a material that everything is using. you dont use a different material. you just change it to your needs. unless you want to manually find every object in the scene that is using it.
careful though. doing this can permanently change your material setting in the asset folder. so back up your material folder before playing with any code like this.
Answer by DarthHawk13 · May 20, 2017 at 04:08 AM
I believe a way to do this is search through all GameObjects in the scene looking for the material you want to replace on each individual GameObject. When a GameObject has that material you're looking for then replace the material on that GameObject.
The code looks like you want to search for all the Renderers in the scene so you can change the material attached to them but each Renderer is a component of a Gameobject. I believe you need to access each GameObject's Renderer through each individual GameObject and see if the material on the Renderer of each GameObject matches the material you want to replace. Then you can replace it.
You may also be getting an error from declaring rend to be a Renderer at the beginning of your code and later on line 18 trying to make it an array when you didn't declare it as an array on line 11.
This Unity Documentation link will get you closer to achieving your goal:
Go through the videos and scripts of the aspects of code you may not be familiar with. It looks to me you're headed on the right path from the way you're thinking about the problem but may need to freshen up on syntax and how Unity and GameObjects are structured. Going through the beginner and intermediate script tutorials may take a little while but it should be shorter than going through a C# book. As you mentioned you're new to programming going through a C# book would be a rewarding experience also. But you don't have to do all that before solving your problem. You can solve your problem in short order by going through some of the script tutorials.
And don't be afraid of getting things wrong or get frustrated if something's not compiling. When I started out it took me three hours to discover I had a missing closing bracket. That ruined my day because I was sooo frustrated and I didn't need to be.