Search Unity

Change a texture at runtime

Discussion in 'Scripting' started by meeydoo, May 7, 2012.

  1. meeydoo

    meeydoo

    Joined:
    Apr 8, 2012
    Posts:
    28
    hey,

    I' trying to change a texture at runtime.

    I have a plane with a texture on it and i want to load another texture so i used this code

    Code (csharp):
    1. public Texture2D change;
    2.  
    3. renderer.material.mainTexture = change;
    I also tried it with a new material but this had no effect.



    http://i.imgur.com/zz9ZM.jpg

    I can see the texutre on the plane in the scene view, but its hidden ingame.

    Any ideas ?
     
  2. cristian_ritter

    cristian_ritter

    Joined:
    Aug 11, 2011
    Posts:
    37
    Try this:

    Code (csharp):
    1. public Texture2D change;
    2.  
    3. renderer.sharedMaterial.mainTexture = change;
    4.  
     
    Last edited: May 7, 2012
  3. meeydoo

    meeydoo

    Joined:
    Apr 8, 2012
    Posts:
    28
    Hi,

    than i get a NullReferenceException, even if i've set the texture2d in the editor.
     
  4. meeydoo

    meeydoo

    Joined:
    Apr 8, 2012
    Posts:
    28
    nvm, i accidentally had two materials on the mesh renderer :X sry guys