Sprite Alpha Color Won't Change Via Script
Update: It's a problem with the Animator taking priority over the sprite renderer. I'm looking into some kind of solution.
I'm trying to code some invincibility frames in my health script and I'm trying to set the Player's alpha value to half while in those frames. The ignoring collision between layers works fine on collision so I know the CoRoutine is running, but I can't get the alpha value to change on runtime. It's at the bottom of the script under the "GetInvulnerable" command. Is it perhaps something wrong with the way I'm calling the Renderer?
public class Health : MonoBehaviour
{
public Image healthBar;
[SerializeField]
private float maxHealth = 100;
private float currentHealth;
float lerpSpeed;
Renderer rend;
Color color;
private void Start()
{
currentHealth = maxHealth;
rend = GetComponent<SpriteRenderer> ();
color = rend.material.color = color;
}
private void Update()
{
if (currentHealth > maxHealth) currentHealth = maxHealth;
HealthBarFiller();
lerpSpeed = 3f * Time.deltaTime;
}
//SMOOTH HEALTH BAR
void HealthBarFiller()
{
healthBar.fillAmount = Mathf.Lerp(healthBar.fillAmount, currentHealth / maxHealth, lerpSpeed);
}
//DAMAGE
public void Damage (float amount)
{
currentHealth -= amount;
if(currentHealth <= 0.0f)
{
Die();
}
}
//HEAL
public void Heal(float amount)
{
if (currentHealth < maxHealth)
{
currentHealth += amount;
}
else
{
currentHealth = maxHealth;
}
}
private void Die()
{
//transition to Death Screen
}
//Invulnerable
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy1" && currentHealth > 0)
StartCoroutine ("GetInvulnerable");
}
IEnumerator GetInvulnerable()
{
Physics2D.IgnoreLayerCollision (8, 12, true);
color.a = 0.5f;
rend.material.color = color;
yield return new WaitForSeconds (2f);
Physics2D.IgnoreLayerCollision (8, 12, false);
color.a = 1f;
rend.material.color = color;
}
}
Answer by tylerdtrudeau · Mar 05, 2021 at 03:41 PM
couple things to try at first glance. at the top of your class try changing "Renderer rend;" to "SpriteRenderer rend;". Then in start do "color = rend.color;" (dont add the extra "= color" at the end). Finally at the bottom of the script replace both instances of "rend.material.color" with "rend.color". Also one thing to keep in mind is that if your sprite on your player is a child of the player game object then in the start method you would have to do "rend = GetComponentInChildren<SpriteRenderer>();"
Let me know how that works
Thanks for the suggestions. Unfortunately none of that seems to have worked, I still can't get the alpha channel to change.
I will attach the code I used just to make it clearer how I did it. Keep in $$anonymous$$d I put an empty game object in the hierarchy and then attached my sprite to that. This is why I am using GetComponentInChildren to get the sprite renderer, as this script is attached to the game object itself.
public class Controller : MonoBehaviour
{
private SpriteRenderer rend;
private Color color;
void Start()
{
rend = GetComponentInChildren<SpriteRenderer>();
color = rend.color;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
color.a = 0.5f;
rend.color = color;
}
if (Input.GetKeyDown(KeyCode.H))
{
color.a = 1f;
rend.color = color;
}
}
}
When I hit "L" the alpha gets cut in half then when I hit "H" the alpha is back to full. Just adapt this to your usage. I have tested it and it works exactly as exepected. If you still cant get it to work after making all the changes I suggested, can you post your updated code so I can see if there is an error somewhere. Cheers
Thanks so much for helping me out. I'm narrowing down what the issue is.
So I added your script to the player and disabled all other components attached to it and it worked fine. Then gradually I turned each component back on until it stopped working. It turns out that the Animator component is the problem. If i turn off the Animator, both your script and my original one work fine in terms of changing the alpha, so the problem lies in the Animator somewhere. If I manually change the alpha or color of the sprite renderer in the viewer, all of the animations change to the new values, but trying to get them to change through a script, your's or $$anonymous$$e, doesn't work with the Animator enabled.
I looked it up and apparently this has been a super common problem since like 2013 and Unity has just never fixed it. The Animator just takes priority over the renderer and it looks like there's no easy solution. How strange.
Your answer
Follow this Question
Related Questions
Error CS0176 on Renderer 1 Answer
My code has some invalid arguments 1 Answer
Change colors for beginners 2 Answers
Setting an Objects Colour using a variable Color from an array? 0 Answers
Accessing shader colors 1 Answer