Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Material color changed at runtime does not match

Discussion in 'Scripting' started by jma00049, Jul 7, 2021.

  1. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    Hello, I am changing some objects material color by script but the color assigned does not match in game view. If I modify again the color in the inspector at runtime and I assign the same color, it matches. Also, in scene view, objects are black until I modify the color in the inspector.

    This is a object in scene view:


    This is the same object in game view:


    This is the color that has been assigned by the script. It's red but the object does not look like red. In scene view is black and if you look at the right-bottom, the material ball looks like pink:


    If I modify the color at runtime and I assign the same color (red). The object in scene view matches and also the material ball at the right-bottom:

    Anyone knows what is happening? Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    When you read from a material in code, you get a copy. That's no longer what is connected in the inspector. You can use the .sharedMaterial instead and you will be changing the original material in code.
     
    jma00049 likes this.
  3. jma00049

    jma00049

    Joined:
    Feb 21, 2019
    Posts:
    89
    That works!. Thank you!
     
    Kurt-Dekker likes this.