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

Can't set color if Animator is active

Discussion in 'Animation' started by VincentRoumier, Sep 28, 2016.

  1. VincentRoumier

    VincentRoumier

    Joined:
    Jun 10, 2013
    Posts:
    8
    Hi,
    I have stumbled onto an issue recently : I'm trying to make an animation that make a 2D Object disappear, by decreasing the alpha. For that, I made an animation :


    Then I affect an animator on my object :


    Now I want to be able to change the color of my sprite via the renderer.

    The issue is, while my Animator component is active, I cannot change it at all. I'm guessing the idle state is reseting the values ( it has None Motion ). Is there a way to go around this?

    I don't want to make an animation that changes the rgb, because if I want to change the colors, I'll have to go over all the animations again.

    Thanks !
     

    Attached Files:

    rakkarage likes this.
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    In unity, renderer.Color is defined in the exact way as transform.position, but instead of xyz it has rgba. If you try and set the color component from script, you will see that you can't set only the alpha channel, as you need to create a new object in which you give: new Color(oldrR, oldG, oldB, newAlpha).

    My point is that Unity displays in animator SpriteRenderer.Color.a for simplicity and easiness of use, however, they need to change all 4 components of color. So, to get to the question, no, you can't both change color and animate alpha in the same time. However, instead of alpha, you can add a script to the respective object with a public variable (lets call it public float Alpha). You can animate this value "Alpha" from animator, then in Update(), update your image alpha + add your own color.
     
    VincentRoumier likes this.
  3. VincentRoumier

    VincentRoumier

    Joined:
    Jun 10, 2013
    Posts:
    8
    Yeah I had the issue with transform and added a parent as a workaround, but didn't know how to do it for the color.
    I'll try your solution, it seems like the best/quickest way here.

    I just want to say that this is a very common thing we want to do ( making a sprite disappear ), and this seems a little overkill.

    Thanks for your help, I'll come back if this doesnt work