Search Unity

Changing the Texture of a Skinned Mesh Renderer

Discussion in 'Scripting' started by GrimWare, Mar 20, 2012.

  1. GrimWare

    GrimWare

    Joined:
    Jul 23, 2009
    Posts:
    211
    Hello,

    I've got this code that will change the texture of an object upon collision, but it won't change the texture of the Skinned Mesh Renderer. I cannot find a way around this, so if you'll take a look at my code maybe we can all figure it out.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class changeToHelmColor : MonoBehaviour {
    5.     public Texture normTexture;
    6.     public Texture helmTexture;
    7.     public Collider helm;
    8.  
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.         renderer.material.mainTexture = normTexture;
    13.         //other.tag = "Helm";
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.     }
    20.    
    21.     void OnTriggerEnter(Collider other) {
    22.         helm = other;
    23.  
    24.         if(other.tag != "Helm") {
    25.             print ("nice try");
    26.         } else {
    27.             renderer.material.mainTexture = helmTexture;
    28.         }
    29.     }
    30. }
    See the Skinned Mesh Renderer isn't at the root of the model. It's a child of the 3d model called Global.

    It works great on every object except the one I want to change :confused:
     
  2. TaintedNobodies

    TaintedNobodies

    Joined:
    Feb 21, 2010
    Posts:
    76
    Add a gameobject variable at the top

    then in the else

    put (name of game object variable).renderer.material.mainTexture = helmTexture;

    and in the inspector attach whatever object you are changing into the variable.
     
  3. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class changeToHelmColor : MonoBehaviour {
    5.     public Texture normTexture;
    6.     public Texture helmTexture;
    7.     public Collider helm;
    8.     private SkinnedMeshRenderer skinnedMeshRenderer; // skinned mesh renderer member variable
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.         skinnedMeshRenderer = GetComponentInChildren<SkinnedMeshRenderer>();  // get the first Skinned mesh renderer component (only active ones!) in the objects children
    13.         skinnedMeshRenderer.material.mainTexture = normTexture;
    14.         //other.tag = "Helm";
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.        
    20.     }
    21.    
    22.     void OnTriggerEnter(Collider other) {
    23.         helm = other;
    24.  
    25.         if(other.tag != "Helm") {
    26.             print ("nice try");
    27.         } else {
    28.             skinnedMeshRenderer.material.mainTexture = helmTexture;
    29.         }
    30.     }
    31. }
    Just get the first active SkinnedMeshRenderer instance using GetComponentInChildren. If have more (i.e. because your skinned mesh consists from many smaller ones), you may want to use GetComponentsInChildren instead.

    http://unity3d.com/support/document...etComponentInChildren.html?from=MonoBehaviour

    Otherwise, consider using Transform.Find("child/which/contains/skinnedMeshRenderer") to find the child object containing the Skinned mesh you wish to change and use GetComponent<SkinnedMeshRenderer>() to get a reference to it.