I am a novice in unity Unity.
In unity i am having a I have a Game Object named 'courtsuit' .'courtsuit'; you can see it in my Scene Hierarchy panel here:
It's in the hierarchy panel
That game object was created from a OBJ file that i dragged to Hierarchy panel
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
manto the game object at runtime .
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
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 can't do using a external onclick script. Because I don't know how to locate the object in Hierarchy panel in program
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 to How can I change the Game Object courtsuit's material to a material named 'man' infrom my Resources/Material folderfolder using C# ?

