Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing the mainTexture of a Instantiated prefab

Discussion in 'Scripting' started by NikLever1, Sep 24, 2013.

  1. NikLever1

    NikLever1

    Joined:
    Aug 23, 2012
    Posts:
    16
    I have a project where initially there will be no Game Objects in the scene hierarchy other than a control script. I have several prefabs that share a script whose purpose is to switch the main diffuse texture. The script has 8 slots to define the textures to use. When I use this script to switch the texture the prefab object that I am updating switches to 'Type mismatch' while the game is running and then the materials that were assigned to the prefab become 'Missing (Material)' when the game stops running. I am on a Mac running 3.5.7f6. I do not see the texture change in the game view. However, if the object is selected then the small preview window does show the changes to the texture. I am totally confused.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    it would be more useful to see the relevant part of the script than have it described...
     
  3. NikLever1

    NikLever1

    Joined:
    Aug 23, 2012
    Posts:
    16
    bool updateTexture(int id){
    Debug.Log (TAG + "updateTexture to " + id + " of " + this.textures.Count);
    //count++;
    if (id<0 || id>textures.Count){
    return false;
    }
    Texture texture = textures[id];
    if (texture == null){
    Debug.Log (TAG + "updateTexture texure is null");
    }else{
    Debug.Log (TAG + "updateTexture to " + texture.ToString());
    Transform tran = gameObject.transform.GetChild (1);
    if (tran==null){
    Debug.Log (TAG + "updateTexture child not found");
    }else{
    GameObject obj = tran.gameObject;
    if (obj==null){
    Debug.Log (TAG + "changeTexture gameObject not found");
    }else{
    MeshRenderer currentRenderer = obj.GetComponent<MeshRenderer>();
    if (currentRenderer != null){
    currentRenderer.material.mainTexture = texture;
    Debug.Log (TAG + "updateTexture " + currentRenderer.materials.Length + " materials amended");
    texId = newTexId;
    }else{
    Debug.Log (TAG + "updateTexture no MeshRenderer");
    return false;
    }

    return true;
    }
    }
    }
    return false;
    }


    Sorry about the formatting - it seems to trash the identing
     
    Last edited: Sep 24, 2013
  4. NikLever1

    NikLever1

    Joined:
    Aug 23, 2012
    Posts:
    16
    Problem appears to be the way I am calling a script attached to a prefab that has been created using Instantiate. More research needed about the scope of scripts using Instantiation. If I restrict all my code to the script for a prefab it all works fine. If instead I try to call the scripts in the instantiated prefabs using a master control script, I get the issues I have been having problems with.
     
  5. Toerktumlare

    Toerktumlare

    Joined:
    Sep 15, 2013
    Posts:
    74

    Click the "Go Advanced" button in the bottom ricght and then click the "Wrap code tags around selected text" button.

    Code (csharp):
    1.  
    2. bool updateTexture(int id){
    3.  
    4.    Debug.Log (TAG + "updateTexture to " + id + " of " + this.textures.Count);
    5.    
    6.    //count++;
    7.    if (id<0 || id>textures.Count){
    8.       return false;
    9.    }
    10.    
    11.     Texture texture = textures[id];
    12.     if (texture == null){
    13.  
    14.         Debug.Log (TAG + "updateTexture texure is null");
    15.  
    16.     }else{
    17.  
    18.         Debug.Log (TAG + "updateTexture to " + texture.ToString());
    19.         Transform tran = gameObject.transform.GetChild (1);
    20.      
    21.         if (tran==null){
    22.  
    23.            Debug.Log (TAG + "updateTexture child not found");
    24.  
    25.         }else{
    26.  
    27.             GameObject obj = tran.gameObject;
    28.          
    29.             if (obj==null){
    30.  
    31.                 Debug.Log (TAG + "changeTexture gameObject not found");
    32.          
    33.             }else{
    34.            
    35.                 MeshRenderer currentRenderer = obj.GetComponent<MeshRenderer>();
    36.            
    37.                 if (currentRenderer != null){
    38.  
    39.                    currentRenderer.material.mainTexture = texture;
    40.                    Debug.Log (TAG + "updateTexture " + currentRenderer.materials.Length + " materials amended");
    41.                    texId = newTexId;
    42.  
    43.                 }else{
    44.  
    45.                     Debug.Log (TAG + "updateTexture no MeshRenderer");
    46.                     return false;
    47.             }
    48.                    
    49.             return true;
    50.         }
    51.      }
    52.    }
    53.    return false;
    54. }
     
  6. NikLever1

    NikLever1

    Joined:
    Aug 23, 2012
    Posts:
    16
    Fixed this. My issue was the way I was Instantiating the prefab not the texture switching code.