Search Unity

Question Initialize Prefab in Project Window

Discussion in 'Prefabs' started by DirtyDilara, Jul 25, 2021.

  1. DirtyDilara

    DirtyDilara

    Joined:
    Jun 7, 2019
    Posts:
    9
    Dear community,

    I have got a problem overhere with my custom prefab.
    On my prefab, i have a script which changes the materials of some render child gameobjects,
    when the script gets initialized or a script value got changed in the inspector.

    However, the unity preview window works fine, everything in the scene too.
    But when viewing the project-tab, i get these annoying purple "No data"-materials (view attachement):
    upload_2021-7-25_10-14-19.png

    So, and here is my"render script":
    -
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteAlways]
    6.  
    7. public class BlockRenderer : MonoBehaviour
    8. {
    9.     BlockInfo tmp_blockInfo_before;
    10.     public BlockInfo blockInfo;
    11.     public MeshRenderer top;
    12.     public MeshRenderer bottom;
    13.     public MeshRenderer front;
    14.     public MeshRenderer back;
    15.     public MeshRenderer left;
    16.     public MeshRenderer right;
    17.  
    18.     private void OnValidate()
    19.     {
    20.         Render();
    21.     }
    22.  
    23.     void Render()
    24.     {
    25.  
    26.  
    27.         foreach (MeshRenderer renderer in new MeshRenderer[] { top, bottom, front, back, left, right })
    28.         {
    29.  
    30.             Texture tex = null;
    31.  
    32.             if (blockInfo != null)
    33.             {
    34.  
    35.                
    36.  
    37.                 if (renderer == top && blockInfo.top != null)
    38.                 {
    39.                     tex = blockInfo.top;
    40.                 }
    41.                 else if (renderer == bottom && blockInfo.bottom != null)
    42.                 {
    43.                     tex = blockInfo.bottom;
    44.                 }
    45.                 else if (blockInfo.main != null)
    46.                 {
    47.                     tex = blockInfo.main;
    48.                 }
    49.  
    50.             }
    51.             else
    52.             {
    53.                 tex = null;
    54.             }
    55.  
    56.             Material mat = new Material(Shader.Find("Diffuse"));
    57.             mat.SetTexture("_MainTex", tex);
    58.             renderer.sharedMaterial = mat;
    59.  
    60.         }
    61.  
    62.        
    63.     }
    64.  
    65. }
    66.  

    Any suggestions overthere?
    For more details, please notify also:)

    Best regards
     
  2. DirtyDilara

    DirtyDilara

    Joined:
    Jun 7, 2019
    Posts:
    9
    upload_2021-7-25_18-12-23.png

    Ok, for some reason, it is working now.
    But you can still make suggestion when getting an idea:)