-1
\$\begingroup\$

is it possible to obtain a material from another object? I'm trying everything and nothing, the only thing I could get was just the color of another object

public MeshRenderer takematerial;
public GameObject[] Objects;
for (int i = 0; i < Objects.Length; i++) {
    Objects [i].GetComponent<Renderer> ().material = takematerial.material;
}
\$\endgroup\$
2
  • \$\begingroup\$ Hi there, just did a google, have you seen that one? answers.unity.com/questions/59355/… \$\endgroup\$
    – D.Kallan
    Jan 6, 2021 at 7:07
  • 1
    \$\begingroup\$ The code you have there copies the material from the takematerial renderer and assigns it to each object in the Objects array. If that's not doing what you want, can you edit your question to add more detail about the observable symptoms by which this differs from what you need? \$\endgroup\$
    – DMGregory
    Jan 6, 2021 at 14:02

1 Answer 1

0
\$\begingroup\$

You need to reach or interact with the target object, by raycast, tag, collision, etc, it doesn't matter:

GameObject targetObj=GameObject.FindWithTag("Player");

Material targetMat = targetObj.GetComponent<Renderer>().material;
//Creates  a copy of the first material of the object,unique to this renderer, if it had been a shared asset previously.

targetMat.color=Color.red; 
//Now targetMat referencing targetObjects renderer first material.


\$\endgroup\$
0

You must log in to answer this question.

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