Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cannot set color of SpriteRenderer at runtime.

Discussion in '2D Experimental Preview' started by akuseix, Nov 9, 2016.

  1. akuseix

    akuseix

    Joined:
    Nov 9, 2016
    Posts:
    2
    I've verified that this is an issue in 5.5.0a6 and the issue does not exist in 5.4.2f2. If you have a GameObject with a SpriteRenderer and try to change the color property at runtime in a script, it will change the color of the SpriteRenderer in the Inspector but will not change the color of the sprite itself in the scene, game view or running build.

    If you manually change the color through the Inspector, it changes correctly in the scene view and game view. It is very easy to reproduce this, just create a script that changes SpriteRenderer.color in the Update function and attach it to the GameObject that has a SpriteRenderer.
     
  2. akuseix

    akuseix

    Joined:
    Nov 9, 2016
    Posts:
    2
  3. Ratatoeskr

    Ratatoeskr

    Joined:
    Oct 25, 2013
    Posts:
    9
    Can confirm. I also have this issue. It doesn''t mater weather you change the color property of the sprite renderer or change the "_Color" property of the material. Neither works.
    Today I switched back to beta 5.5 and.. tada! suddenly my animated sprite colors worked again :p
     
  4. Fuzzy

    Fuzzy

    Joined:
    Feb 11, 2011
    Posts:
    266
    Already reported this with an example project using the bug reporter on 24.09.2016
    Zero reaction so far.
    case no: 834911
     
  5. Johaness_Reuben

    Johaness_Reuben

    Unity Technologies

    Joined:
    Jan 27, 2016
    Posts:
    253
    Probably something related to the version Preview2 was based on. Experimental Preview 3 is based on 5.5 beta so it should work fine.
     
  6. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,511
    Hi.

    I have similar issue related to this.

    The changing of spriterenderer's color property does seems to work at first, but then when you change its material or shader to something else that is also based on the default sprite shader at runtime, it does not change the color of the sprite.

    I have confirmed this with changing the shader of the sprite's renderer material to the exact same copy of the default sprite shader and the shader does not receive the vertex color information from the spriterenderer.

    However, setting the _Color value of the material does seem to work.

    This happens only when you change spriterenderer's material shader into something else during runtime. Even if the changed shader is actually the same shader as the default sprite shader.

    Something odd is happening when spriterenderer's material's shader changes.. perhaps the spriterenderer is not expecting such changes during runtime?
     
  7. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    301
    Has this been fixed? Three years later I'm experiencing the same thing. Setting SpriteRenderer.color = new Color(255,215,215); results in the property being set to r=192, g=162, b=162 in the inspector but no update in the scene.
     
  8. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    301
    It's a bug in the the integer overload of the Color constructor. Using new Color(255/255f, 215/255f, 215/255f) resulted in normal behavior.
     
    Masticater and luigitortellini like this.
  9. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Hello @cj-currie,
    Could you create a bug report for this issue? Then we'll take a look.
    Thanks!
     
  10. Cambesa

    Cambesa

    Joined:
    Jun 6, 2011
    Posts:
    119
    Any news on this? I'm setting alpha by code but it only starts working after you modify the color once by hand after clicking play
     
  11. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Hello @Cambesa,
    We haven't received any bug report to my knowledge regarding this issue. Could you file one so we can take a look?
    Thanks!
     
  12. TheCheesonOne

    TheCheesonOne

    Joined:
    May 30, 2020
    Posts:
    3
    Hi @Ted_Wikman,
    I am having the same problem and have just submitted a bug report.
    Thanks in advance for your help!
     
    Ted_Wikman likes this.
  13. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    TheCheesonOne likes this.
  14. TheCheesonOne

    TheCheesonOne

    Joined:
    May 30, 2020
    Posts:
    3
    Hi @Ted_Wikman,
    Have upgraded to unity 2021 today but the problem persists, any news on this issue?
    Thanks!
     
  15. TheCheesonOne

    TheCheesonOne

    Joined:
    May 30, 2020
    Posts:
    3
    Hi all,

    Have just realised there was no bug (in my case anyway). I was attempting to alter the .material.mainTexture property of my <SpriteRenderer>, when in fact I should have been setting it's .sprite property to a newly created sprite.


    The first answer on this post contains the solution:
    https://forum.unity.com/threads/how-to-change-the-sprite-of-an-object-pixel-by-pixel.437756/

    Code (CSharp):
    1. Texture2D texture = new Texture2D(128, 128);
    2. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, 128, 128), Vector2.zero);
    3. GetComponent<SpriteRenderer>().sprite = sprite;
    4.  
    5. texture.SetPixels() #do something here
    6. texture.Apply()
    Thanks all, hope this can close my report @Ted_Wikman
     
  16. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    875
    Great that you found a fix to your issue, and thanks for posting it here.
    You can add this information to the bug ticket as well to let the Unity QAs know about your solution.
    Thanks!
     
    TheCheesonOne likes this.