0
\$\begingroup\$

Im using an asset I bought some time ago, and using some models from it. This is the model:

enter image description here

My question is, how could I dynamically change things like the hair, the uniform, etc? The asset comes with various PNG files for every part of the model (face, hair, shoes, etc.) and I would like the player to be able to shoose a hair, a skin, etc. for the model. I see a list of materials slots in the Inspector where they are set, but don't know how to do it programatically.

EDIT:

I (sort of) achieved what I wanted, this way:

if(Input.GetKeyDown(KeyCode.T)){
        GameObject polyMesh = selectedUnit.transform.Find("Polymesh").gameObject;
        GameObject model=polyMesh.transform.Find("model_001").gameObject;
        SkinnedMeshRenderer mesh=model.GetComponent<SkinnedMeshRenderer>();
        Material[] materials = mesh.GetComponent<Renderer>().materials;
        Material WhiteUniform = Resources.Load<Material>("Materials/WhiteShirt");
        materials[1] = WhiteUniform;
        mesh.GetComponent<Renderer>().materials=materials;
    }

Now my problem is that it changes the shirt of all the players. It is possible to change the material of only the selected player?

\$\endgroup\$
3
  • \$\begingroup\$ I'd assume you tried reading the renderer.materials array, replacing the materials you wanted to change, then assigning the result back? Where did you run into difficulty making this work for your use case? \$\endgroup\$ Commented Nov 16, 2022 at 16:57
  • \$\begingroup\$ I didn't actually tried, I am fairly new to Unity and didn't know how to access the materials. By using renderer.materials array, and assigning the materials I create from the textures in the asset to every slot in the array would work? Do they come in the same order that is shown in the Inspector? I should access first to the Player object, then to the Polymesh object and finally to model_001 object, right? \$\endgroup\$ Commented Nov 16, 2022 at 17:12
  • 1
    \$\begingroup\$ Those sound like questions you can answer for yourself by trying it. If you hit a snag or unexpected behaviour, edit your question to show what you tried and where you need help. \$\endgroup\$ Commented Nov 16, 2022 at 17:14

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.