I am a novice in Unity. I have a Game Object named 'courtsuit'; you can see it in my Scene Hierarchy panel here:
I added that object instance to my scene by dragging an OBJ file from my Project Assets folder into the Scene Hierarchy panel.
Now I want apply a material man to the game object at runtime. That is, when I click a button, that game object needs to change its material.
I tried and succeed by attaching a script to game object itself:
Material SphereMaterial;
// Use this for initialization
void Start()
{
SphereMaterial = Resources.Load<Material>("Materials/man");
if (SphereMaterial == null) Debug.Log("mat null");
else Debug.Log("mat not null");
Debug.Log("new Material: " + SphereMaterial.name);
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
// Get the current material applied on the GameObject
Material oldMaterial = meshRenderer.material;
Debug.Log("Applied Material: " + oldMaterial.name);
// Set the new material on the GameObject
meshRenderer.material = SphereMaterial;
}
But I don't know how to do this from an OnClick script on a different object, because I don't know how to locate the object in my Scene Hierarchy using C# script.
I want to do something like this:
public class ButtonHandler : MonoBehaviour {
public void ButtonInteract()
{
Debug.Log("changing matriel ");
// Change the material of the game object here
}
}
How can I change the Game Object courtsuit's material to a material named 'man' from my Resources/Material folder using C#?

