Search Unity

Can't change color of objects

Discussion in 'Scripting' started by Avian_Overlord, Feb 21, 2018.

  1. Avian_Overlord

    Avian_Overlord

    Joined:
    Dec 27, 2015
    Posts:
    34
    This is a bit of code from my procedural building generation. I get a NullReferenceException for the two marked lines (37 & 38). I'm not using the default shader on the wall objects. Can anyone see what the problem might be?

    Code (CSharp):
    1. public class WallBehavior : MonoBehaviour {
    2.  
    3.     public Divider data;
    4.  
    5.     Renderer wallA;
    6.     Renderer wallB;
    7.  
    8.     public void Start()
    9.     {
    10.         Renderer[] walls = GetComponentsInChildren<Renderer>();
    11.         wallA = walls[0];
    12.         wallB = walls[1];
    13.     }
    14.  
    15.     public void Setup()
    16.     {
    17.         if(data == null)
    18.         {
    19.             return;
    20.         }
    21.  
    22.         BuildingCell cellA;
    23.         BuildingCell cellB;
    24.         if(data.vertical)
    25.         {
    26.             cellA = data.cell1;
    27.             cellB = data.cell2;
    28.         }
    29.         else
    30.         {
    31.             cellA = data.cell2;
    32.             cellB = data.cell1;
    33.         }
    34.  
    35.         Color colorA = GetColor(cellA);
    36.         Color colorB = GetColor(cellB);
    37.         wallA.material.color = colorA;//BUG HERE
    38.         wallB.material.color = colorB;//BUG HERE
    39.      
    40.     }
    41.  
    42.     public Color GetColor(BuildingCell cell)
    43.     {
    44.         switch (cell.mainRegion.regionTag)
    45.         {
    46.             case "OUTDOORS":
    47.                 return Color.grey;
    48.             case "BATHROOM":
    49.                 return Color.cyan;
    50.             case "BEDROOM":
    51.                 return Color.red;
    52.             case "BEDROOM1":
    53.                 return Color.green;
    54.             case "HALLWAY":
    55.                 return Color.yellow;
    56.             case "LIVING":
    57.                 return Color.blue;
    58.             case "KITCHEN":
    59.                 return Color.magenta;
    60.             case "DINING":
    61.                 return Color.black;
    62.         }
    63.         return Color.clear;
    64.     }
    65. }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    works for me.. try printing them out at Start and before assigning colors to see if they somehow are null,
    Code (CSharp):
    1. Debug.Log(wallA);
    2. Debug.Log(wallB);
    3.  
     
    Avian_Overlord likes this.
  3. Avian_Overlord

    Avian_Overlord

    Joined:
    Dec 27, 2015
    Posts:
    34
    Okay, they work fine in start but return null before assigning colors. Thank you. Any idea what might cause something like this?

    Edit: Problem resolved. Setup was being called before Start.
     
    Last edited: Feb 21, 2018