Skip to main content
Cleanup
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

How to loadreference a gameobject inGameObject from the Scene Hierarchy in C#?

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

courtsuit game object

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.

obj file name and material name

Now I want apply a material man to 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#  ?

How to load a gameobject in Hierarchy in C#?

I am a novice in unity .

In unity i am having a Game Object named 'courtsuit' .

It's in the hierarchy panel

courtsuit game object

That game object was created from a OBJ file that i dragged to 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

 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

public class ButtonHandler : MonoBehaviour {
 

    public void ButtonInteract()
    {
        Debug.Log("changing matriel ");
        // Change the material of the game object here

    }
}



How to change the Game Object courtsuit's material to a material named 'man' in Resources/Material folder using C#  ?

How to reference a GameObject from the Scene Hierarchy in C#?

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

Source Link
RagAnt
  • 105
  • 4

How to load a gameobject in Hierarchy in C#?

I am a novice in unity .

In unity i am having a Game Object named 'courtsuit' .

It's in the hierarchy panel

courtsuit game object

That game object was created from a OBJ file that i dragged to 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

 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

public class ButtonHandler : MonoBehaviour {


    public void ButtonInteract()
    {
        Debug.Log("changing matriel ");
        // Change the material of the game object here

    }
}



How to change the Game Object courtsuit's material to a material named 'man' in Resources/Material folder using C# ?