0
\$\begingroup\$

I'm trying to insert multiple materials into the object's submesh using the array, but I'm not getting enter image description here enter image description here

public GameObject[] obj;

//material mat;

public void Color(Material mat) {

    for (int x = 0; x < obj.Count; x++)
        obj[x].GetComponent<MeshRenderer>().sharedMaterials[1] = mat;

    Material[] mats = GetComponent<Renderer>().materials;
    mats[1] = mat;
    GetComponent<Renderer>().materials = mats;

    {
        print("New Material");
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ Sorry, Can you be more specific of what you are trying to do?? \$\endgroup\$ Feb 7, 2019 at 10:10
  • \$\begingroup\$ Your second attempt here — capturing a mats array, setting its entry at a particular index, then assigning the modified array back to the renderer — this looks correct. Is it not performing the way you expect? If not, can you explain in detail what you want it to do, and what behaviour you're observing instead? \$\endgroup\$
    – DMGregory
    Feb 7, 2019 at 13:29
  • \$\begingroup\$ from the material line [] mats, it's only 1 material for the respective element, so I'm trying to insert an array of materials, but I'm not getting \$\endgroup\$
    – Nitecki
    Feb 7, 2019 at 18:49
  • \$\begingroup\$ Please try to explain that in more detail.The language barrier here is making it difficult to understand what you mean. Maybe try editing your question to give an example of what you want the materials array to contain before your code runs, then show an example of what you want the array to contain after your code has run. Then we can see clearly what change you're trying to make. \$\endgroup\$
    – DMGregory
    Feb 8, 2019 at 3:24
  • \$\begingroup\$ I'm sorry, see if you can understand now. \$\endgroup\$
    – Nitecki
    Feb 8, 2019 at 4:15

1 Answer 1

1
\$\begingroup\$
material mat; 
GetComponent<MeshRenderer> ().sharedMaterials [1] = mat; 
Material[] mats = GetComponent<Renderer>().materials; 
mats[1] = mat; 
GetComponent<Renderer>().materials = mats; 

First error: The class "Material" should be in capital letters. (your code shouldnt compile)

Second error:

GetComponent<MeshRenderer> ().sharedMaterials [1] = mat; 

doesnt do anything. You can not assign that material like this.

The rest of your code looks correct.

  1. Did you assign the "mat" in the inspector?
  2. Does your mesh even have more than 1 submesh?
  3. Do you even want to assign more than 1 material?
  4. Whats the error ? Doesnt it compile? Null reference? Nothing happenes?
  5. Does your mesh renderer get the materials or does it just look wrong in the 3d view?

enter image description here

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

public class MaterialArraySetter : MonoBehaviour {

    public Material Material1;

    void Start ()
    {
        Material[] materialsArray = GetComponent<Renderer>().materials;
        materialsArray[1] = Material1;
        GetComponent<Renderer>().materials = materialsArray;

    }

}

enter image description here

EDIT2:

You seem to have compiler errors. enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ type is because I'm trying to use an array of gameobject or of materials either, so the object receives the color the script itself works like this, but with only one material settling on its due. material mat; GetComponent<MeshRenderer> ().sharedMaterials [1] = mat; Material[] mats = GetComponent<Renderer>().materials; mats[1] = mat; GetComponent<Renderer>().materials = mats; I'm trying to insert an array of materials, but I can not \$\endgroup\$
    – Nitecki
    Feb 7, 2019 at 18:42
  • \$\begingroup\$ i edited my answer \$\endgroup\$
    – OC_RaizW
    Feb 8, 2019 at 0:14
  • \$\begingroup\$ Do you really want to assign more than one material? is exactly that, I'm trying to set an array of material to assign several materials in the sub-mesh, but I'm not getting... here are the errors i.imgur.com/KIVAw8Y.png \$\endgroup\$
    – Nitecki
    Feb 8, 2019 at 3:54
  • \$\begingroup\$ i edited my answer - again \$\endgroup\$
    – OC_RaizW
    Feb 8, 2019 at 11:02
  • \$\begingroup\$ My God, I'm very blind, I swear to you that I saw a Length kkk, thank you very much. \$\endgroup\$
    – Nitecki
    Feb 8, 2019 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .