accessing materials from skinned mesh renderer
I am trying to swap shaders on objects at runtime when they are clicked on. after they are clicked off i want the shader to be returned to the standard shader and the original material to be reapplied. I have been able to get this to work with a normal mesh shader but only if i set the material in the inspector. I am trying to assign the material that is on the object at startup to be reapplied. I have not been able to do this in any capacity and have not been able to extract an array of materials from an object with children using a SkinnedMeshRenderer. I would really appreciate any help, i have been working on this for many hours with no luck.
This is the important code:
MeshRenderer newRenderer;
SkinnedMeshRenderer[] skinRenderer;
public Material[] mat; // array for materials currently on object and children for MeshRenderer
public Material[] sMat; // array for materials currently on object and children for skinnedMeshRenderer
Renderer[] childMats; // Materials on parent and all applicable children
Shader shader; // standard shader
Shader outline; // Shader I want to swap with
void Start()
{
newRenderer = this.GetComponent<MeshRenderer>();
newRenderer = this.GetComponent<MeshRenderer>();
skinRenderer = this.GetComponentsInChildren<SkinnedMeshRenderer>();
if (newRenderer != null)
{
childMats = this.GetComponentsInChildren<Renderer>();
foreach (Renderer rend1 in childMats)
{
Material[] mat = rend1.materials;
}
}
if (skinRenderer != null)
{
childMats = this.GetComponentsInChildren<SkinnedMeshRenderer>();
foreach (SkinnedMeshRenderer rend1 in childMats)
{
Material[] sMat = rend1.materials;
}
}
}
These next sections of code are called when an object is clicked on (OnFocus) and when it is clicked off of (OnDeFocused):
public void OnFocused (Transform playerTransform)
{
isFocus = true;
player = playerTransform;
hasInteracted = false;
if (newRenderer != null)
{
newRenderer.material.shader = Shader.Find("Outlined/UltimateOutline");
}
if (skinRenderer != null)
{
skinRenderer.materials.shader = Shader.Find("Outlined/UltimateOutline");
}
}
public void OnDeFocused ()
{
isFocus = false;
player = null;
hasInteracted = false;
if (newRenderer != null)
{
newRenderer.material = new Material(Shader.Find("Standard"));
gameObject.GetComponent<Renderer>().materials = mat;
Debug.Log("DeFocus");
}
if (skinRenderer != null)
{
skinRenderer.materials = new Material(Shader.Find("Standard"));
gameObject.GetComponent<Renderer>().materials = mat;
Debug.Log("DeFocus");
}
}
The error I am getting is in the two functions, "OnFocused" and "OnDeFocused" specifically when trying to swap shaderes here:
if (skinRenderer != null)
{
skinRenderer.materials.shader = Shader.Find("Outlined/UltimateOutline");
}
if (skinRenderer != null)
{
skinRenderer.materials = new Material(Shader.Find("Standard"));
gameObject.GetComponent<Renderer>().materials = mat;
Debug.Log("DeFocus");
}
The error is " 'UnityEngine.SkinnedMeshRenderer[]' does not contain a definition for 'materials' and no extension method 'materials' of type 'UnityEngine.SkinnedMeshRenderer[]' could be found. Are you missing an assembly reference?"
I found that there is SkinnedMeshRenderer.materials in the api but cant get it to work. I also cant seem to get the inspector to extract the current materials on the objects.
I hope someone can help me with this, thanks.
You wouldn't change shaders -- you'd change materials.
Shaders have fields for properties that are populated when you create a material from them. You can access and change a Skinned$$anonymous$$eshRenderer's materials whenever and however you want.
$$anonymous$$ake a second set of materials using the other shader and swap them out that way.
Great thanks so much! I haven't worked with shaders or materials much, fixed the code no problem knowing this.