Search Unity

Changing Cylinder Colour Affects Shader

Discussion in 'Scripting' started by gretty, Dec 9, 2013.

  1. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    Hello

    I am attempting to change the colour of a Cylinder at runtime.

    My Problem: When I change the cylinders texture I loose the materials shader. The cylinder now looks flat and has no shadows (so I think its the shader or maybe something else?)

    The cylinder has the material `Default-Diffuse` when the game starts and I change the cylinders material colour to `Color.red` at runtime. See below how the colour changes correctly but the shader disappears - the cylinder looks flat.

    Cylinder look at Application start:
    $19097-capture2.jpg

    Cylinder look after I change the colour:
    $19098-capture.jpg

    What am I doing wrong? And how can I make sure the clylinder still looks 3d/has shadows/has its shader?

    Code - This script is attached to a Cylinder GameObject:

    Code (csharp):
    1. using UnityEngine;
    2.     using System.Collections;
    3.    
    4.     public class Segment : MonoBehaviour {
    5.        
    6.         #region Static Variable
    7.         public static readonly string   SEGMENT_PREFAB_NAME = "concretePipe/ConcretePipePrefab";
    8.         public const double             SEGMENT_SCALE_X     = 0.01f;
    9.         public const double             SEGMENT_SCALE_Y     = 0.01f;
    10.         public const double             SEGMENT_SCALE_Z     = 0.01f;
    11.         #endregion
    12.        
    13.         #region Instance Variables
    14.         #endregion
    15.        
    16.         public virtual void initialise(string name, Transform parent) {
    17.             this.name             = name;
    18.             this.transform.parent = parent;
    19.         }
    20.        
    21.         public virtual bool setColour(Color colour) {
    22.             if (renderer == null)
    23.                 return false;
    24.            
    25.             Debugger.print ("Prior: shader " + renderer.material.shader.ToString()); // Prior: shader Diffuse
    26.             renderer.material.color = colour;
    27.             Debugger.print ("Post: shader " + renderer.material.shader.ToString());  // Post: shader Diffuse
    28.            
    29.             //float lerp = Mathf.PingPong(Time.time, 1f) / 1f;
    30.             //renderer.material.color = Color.Lerp(colour, Color.white, lerp);
    31.             return true;
    32.         }
    33.     }
     
  2. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    :( No Advice?