0
\$\begingroup\$

I am a novice in Unity. I have a Game Object named 'courtsuit'; you can see it in my Scene Hierarchy panel here:

courtsuit game object

I added that object instance to my scene by dragging an OBJ file from my Project Assets folder into the Scene Hierarchy panel.

obj file name and material name

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#?

\$\endgroup\$
2
\$\begingroup\$

The simplest way is to expose a public variable of the types you want to work with:

public class MaterialChangeButtonHandler : MonoBehaviour {

    public MeshRenderer rendererToChange;
    public Material materialToChangeItTo;

    public void ButtonInteract()
    {
        rendererToChange.sharedMaterial = materialToChangeItTo;
    }
}

This will add two editable fields to the Inspector interface for this script. One for a MeshRenderer - you can drag your scene instance of your object into this field, or click the circle next to the field to choose a MeshRenderer from a list of scene objects. The other for the material to apply - you can similarly drag your material from the Assets folder, or click the circle to choose it from a list.

Referencing GameObjects and their components is covered in just about every Unity tutorial out there, so please take the time to work through some beginner's training material to get a hang of the basics of the engine. This will help you form more focused questions when you come across something the tutorials didn't cover.

\$\endgroup\$
  • \$\begingroup\$ thanks I successfully changed material at runtime \$\endgroup\$ – Lord Commander Jun 21 at 6:26
0
\$\begingroup\$

I successfully changed material at runtime

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonHandler : MonoBehaviour {
    GameObject manObj;
    MeshRenderer m;
    // Use this for initialization
    void Start () {
        manObj= GameObject.Find("courtsuit/g_CMan0205_HD2_O01P01_S_CMan0205_HD2_O01P01_S");
        if (manObj== null) Debug.LogError("game obj null");
        else Debug.Log("game NOT null");

        m = manObj.GetComponent<MeshRenderer>();
        if (m == null) Debug.LogError("mesh obj null");
        else Debug.Log("mesh NOT null");
    }

    // Update is called once per frame
    void Update () {

    }

    public void ButtonInteract()
    {
        Debug.Log("Hello chaning matrieal ");
        Material SphereMaterial = Resources.Load<Material>("Materials/man");
        if (SphereMaterial == null) Debug.LogError("mat null");
        else Debug.Log("mat not null");
        Debug.Log("new Material: " + SphereMaterial.name);
        m.material = SphereMaterial;
    }

}
\$\endgroup\$
  • 1
    \$\begingroup\$ I do not recommend looking up objects by name. There are usually more robust alternatives, like storing a reference in an Inspector, or finding it via a specific marker script, or using a service locator of some variety. \$\endgroup\$ – DMGregory Jun 21 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.