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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SpriteRendered refused to change state by code

Discussion in 'Editor & General Support' started by Marzoa, May 5, 2015.

  1. Marzoa

    Marzoa

    Joined:
    Dec 2, 2012
    Posts:
    50
    Hi there,

    I have an SpriteRenderer that refuses to change state by code in a C# script.

    I have this in the Start method of such Script (fooSprite have been defined before as an SpriteRenderer):

    fooSprite = GameObject.Find("fooGameObject").GetComponent<SpriteRenderer>();
    fooSprite.enabled = false;

    I know these lines are being executed by debugging step by step.

    I know it is the right object because I am doing other things with it in the code without problems (specifically rotating and moving it around).

    I know I am not reenabling it elsewhere, because if I disable the sprite in the editor, it keeps disabled all the way. Unsurprisingly, if I try to enable it in the code, the code seems to be ignored too and so the sprite is kept disabled.

    However, I have an animation that does the sprite blink by modifying its renderer's enabled from true to false and vice versa each 1/6 of second, and it does work. So the problem seems to be exclusively from the script code.

    I am even thinking about creating a couple of looped animations, one with the renderer enabled all the time, and the other with it disabled, as a dirty workaround...

    But before doing so, I'd like to ask here if someone have experienced similar problems and have found a better solution. Maybe I need to force something somewhere somehow to refresh or whatever...

    Bests,
     
  2. Marzoa

    Marzoa

    Joined:
    Dec 2, 2012
    Posts:
    50
    OK, I had an enlightenment moment right after writing that.

    It seems it is related with the animator. Setting its culling mode to "Based on renderers" solved this problem, but now the blinking animation doesn't start when it should... whatever, in any case it is a different problem, so I am going to deal with it.

    I leave this here just for the record. Maybe it is useful for someone else...
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    Yeah, if you have animated the enable state,
    then most likely it sets the value after you change it from code..

    Wouldnt it be easier to just set alpha to 0 ? (to make it transparent)
    Or stop the animator while you want to do enable/disable from code.
     
  4. Marzoa

    Marzoa

    Joined:
    Dec 2, 2012
    Posts:
    50
    Hi,

    Sure, I thought about using alpha in the animator, but setting the renderer to true or false was most obvious and slightly easier: you know, as alpha can have intermediate values, I'd have to flat the curve between keys to avoid an unwanted fading effect. It's just a couple of clicks, but is a work you don't need to do with the renderer boolean value.

    Also I had already made the animation that way, so in the end I chose to enable/disable the animator in the C# code where I need to enable/disable the renderer, that's only in two places actually, thus I just had to add a couple of lines of code which is fine enough for me.

    Thanks for your answer,