Search Unity

I changed texture of model, but it doesn't update on game?

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

  1. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    Hello i set up another topic since it two different things, I successfully changed texture on Character (Eyelash) but model during gameplay remains the same (it doesn't matter how many times i changed texture), is there any method that will force engine to redraw mode??

    My Code
    Code (csharp):
    1.  
    2. public void NextEyelash(){  
    3.             int index = -1;
    4.             SkinnedMeshRenderer renderer = Model.transform.Find ("Genesis2Female").transform.Find ("Genesis2Female.Shape").GetComponent<SkinnedMeshRenderer> ();
    5.             Material[] restoreMaterials = new Material[renderer.sharedMaterials.Length];
    6.             restoreMaterials = renderer.sharedMaterials;
    7.             for (int i = 0; i < restoreMaterials.Length; i++) {
    8.                 if (restoreMaterials[i].name.ToString().Contains("lash")){
    9.                     index = i;
    10.                     break;
    11.                 }
    12.             }
    13.             if (index == -1)
    14.                 return;
    15.             string currEyelash = restoreMaterials[index].name.ToString();
    16.             if (!Enum.IsDefined (typeof(Eyelash), currEyelash)) {
    17.                 currEyelash = Eyelash.Eyelash1.ToString();
    18.             }
    19.             Eyelash enumeyelash = (Eyelash)Enum.Parse (typeof(Eyelash), currEyelash);
    20.             int position = (int)enumeyelash + 1;
    21.             if (position >= Enum.GetNames(typeof(Eyelash)).Length){
    22.                 position = 0;
    23.             }
    24.             enumeyelash = (Eyelash)position;
    25.             restoreMaterials[index] = (Material)Resources.Load (enumeyelash.ToString());
    26.             renderer.sharedMaterials = restoreMaterials;
    27.             Debug.Log ("Succes In Changing Eyelash texture :" +restoreMaterials[index].name.ToString());
    28.         }
    29.  
     
  2. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    So after few tries i come to one conclusion, prefabs are totally indenpendent from instance of object draw in game. My example is what i understand when i place in code:
    Code (csharp):
    1.  
    2. renderer.enable = false;
    3.  
    gameObject should dissapear from screen, but it's only happens in scene edit in game window it makes no changes to Model, so my updated question is how to get instance of object created during gameplay from my Model prefabs??
     
  3. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Object myPrefabObject = Resources.Load("myPrefab");
    GameObject myGO = (GameObject) Instantiate ( myPrefabObject , Vector3.zero , Quaternion.identity );

    The first line will load the prefab from the path you give.
    Then in the second line you can create a gameobject from that loaded prefab.

    Hope thats what you are looking for.
     
  4. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    Thanks for reply, although it's not what i ask but still it's a way around to get instance of object, My insecure comes from this that i would need to set all starting position of characters through code, but it's better than doesn't have access to instance.

    ps. I also thought about finding instance by tag but requires different tags for every single npc or any other entity which we also want to be customizable (i talk here about customizable homes available for player).