Search Unity

Help with changing texture of Eyelash dynamically from code(non working code).

Discussion in 'Scripting' started by dullman, Jan 26, 2015.

  1. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    Hello i write a code which should change choosen material (or it's texture) to new one, but after trying few times it seems that when i get access to material it delete reference to any material in assets and after closing up game entire model in unity scene becomes pink and i need to reimport again figure to get textured model. After some inspecting of code (I want similar debug machine as is in visual studio) it's become pink when currently used materials in skinnedMeshedRenderer is copied to array of Materials, after trying another aproach by using method setTexture, it's only one part of body loses it texture (first one in materials), it's only that much information i knew before i went to work. So my questions is why the texture information is lost when i try to access them ( I don't know why it should delete it's value and return to null if anyone access it from code?). Second is how to successfuly change texture to new one (During running code there is no information that it failed to load texture from resources so it should be successful). And third and last when all information in runtime is lost there is no change in model, so how inform engine that it should redraw character with new textures?

    ps. My Code:
    Code (csharp):
    1. Material eyelash = null;
    2. SkinnedMeshRenderer skinnedMeshController = Model.transform.Find("Genesis2Female").transform.Find("Genesis2Female.Shape").GetComponent<SkinnedMeshRenderer>();
    3. Material[] materials = skinnedMeshController.materials;
    4. for (int i = 0; i < materials.Length; i++) {
    5. Debug.Log(materials[i].name.ToString());
    6. if (materials[i].name.ToString().Contains("RyBelle_lashes")){
    7. eyelash = materials[i];
    8. break;
    9. }
    10. }
    11. string currEyelash = eyelash.mainTexture.name.ToString();
    12. Eyelash enumeyelash = (Eyelash)Enum.Parse (typeof(Eyelash), currEyelash);
    13. int position = (int)enumeyelash;
    14. if (position >= Enum.GetNames(typeof(Eyelash)).Length){
    15. position = 0;
    16. }
    17. enumeyelash = (Eyelash)position;
    18. Texture newTexture = (Texture)Resources.Load(enumeyelash.ToString ());
    19. eyelash.mainTexture = newTexture;
    20. skinnedMeshController.materials = materials;
    21. Debug.Log ("Succes In Changing Eyelash texture");
    As i write before everything goes pink in third line of code

    and other attempt to set texture:
    Code (csharp):
    1. Model.transform.Find ("Genesis2Female").transform.Find ("Genesis2Female.Shape").GetComponent<SkinnedMeshRenderer> ().material.SetTexture ("RyBelle_lashes", (Texture)Resources.Load ("EyeLash 1"));
    Here goes pink only first element materials array.

    ps. One thing it's possible to use lambda constructions with unity creations, since i wanted get material from array and it shows some error.
     
  2. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    After some research it shows that every time i use property of materials, materials being set to null, So my new question sounds like that is there a way to set a material in materials array without deleting materials? (In manual it said the we need to restore materials but the question is how to get base state without destroying it?? PLEASE HELP because it's engine breaking bug for me (without customization there is no game)
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    where does this piece of code run? in Update?
    and what exactly are you trying to do... switch eyelash material, but how and when?
    normally something simple as this should work:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SwitchMaterial : MonoBehaviour {
    5.  
    6.     public Material[] m;
    7.     private int i;
    8.  
    9.     void Update ()
    10.     {
    11.         if(Input.GetMouseButtonDown(0))
    12.         {
    13.             i++;
    14.             if(i>=m.Length)
    15.                 i = 0;
    16.             renderer.material = m[i];
    17.         }
    18.     }
    19. }
    20.  
     
  4. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    So what i want to make is switch eyelash texture in a case when we press a button next for eyelash. And your code isn't something that i wanted (I know the unity let me single material change without problem, THE REAL PROBLEM is that when i try access to materials array it become full of null, so i ask myself why and don't have any real answers, as the current state i would consider this as bug of engine, but need some way to rework these in other way then creating array of materials for my body and update that every time i customize character (Which will be also needed to add in clothes and every single item i plan allow to player customize - if they have multiple materials).
    Also you ask how i'm trying to change, the idea is simple every time i click the button, method runs which
    1. access to materials.
    2. Find material for eyelash
    3. Change it to new one
    There is some other steps here but they are just for finding which eyelash is next (or prev since i will copy and paste to have similar function). But problem lies that i want multiple parts of bodies to customize, so every answer sounded like just make body for each possibility will result in thousands models of single character (And i want to make that every time player find new character to customize them to player preference).
     
  5. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    If someone interesting i resolve the problem by accessing to sharedMaterials instead of Materials but what the difference i don't know, although there is a behaviour of shared materials which make me worry.
    First why it's shared? I mean the material is created only once and use by all objects that use it, so if i have for example two actors using the same eyelash and one actor change it, so we get it also on second character or will remain the same besides the fact that two models don't share the material any longer?