Hi everybody, it’s me again !
I’m making a video game in Unity 2D for my final exam (baccalaureate), and I have some little issues with IEnumerators.
In fact, I’m Trying to make my player, when it collides with some Enemy, to flash like three times, but it doesn’t work.
I have this function :
public IEnumerator takendamage(){
renderer.enabled = false;
yield return new WaitForSeconds(takendamage);
renderer.enabled = true;
yield return new WaitForSeconds(takendamage);
renderer.enabled = false;
yield return new WaitForSeconds(takendamage);
renderer.enabled = true;
yield return new WaitForSeconds(takendamage);
renderer.enabled = false;
yield return new WaitForSeconds(takendamage);
renderer.enabled = true;
yield return new WaitForSeconds(takendamage);
}
that I added in my Player Script.
But it seems like enabled isn’t the word, it doesn’t work and when I’m trying to backup, I obtain this script :
public IEnumerator takendamage(){
GetComponent().enabled = false;
yield return new WaitForSeconds(takendamage);
GetComponent().enabled = true;
yield return new WaitForSeconds(takendamage);
GetComponent().enabled = false;
yield return new WaitForSeconds(takendamage);
GetComponent().enabled = true;
yield return new WaitForSeconds(takendamage);
GetComponent().enabled = false;
yield return new WaitForSeconds(takendamage);
GetComponent().enabled = true;
yield return new WaitForSeconds(takendamage);
}
…which is also incorrect.
Some help pleaaaase ?
There is lot of ways to do that and this is probably the most complicated. Use animation instead.
If you want to use this, you have to keep reference to SpriteRenderer…
Use StartCoroutine() to call your function, don’t call it directly. You should also get familiar with loops.
Also, use CODE tags when posting source code.
I think I’ll use that kind of scripting, I’m not at all in fond of animating :')
And @blizzy , what do you mean by code tags ?
And even by StartCoroutine function, do I have to put it out my IEnum. one ?
Uhh, what??
You’re using IEnumerator and WaitForSeconds, so it looks like you are trying to run a coroutine. You use StartCoroutine() to do that.
If that’s not the problem, you should probably explain in more detail what the problem is.
I’ve modified a bit the script, but it seems like “enabled” isn’t recognized by the console…
void Start()
{
StartCoroutine (takendamage);
}
IEnumerator takendamage(){
renderer.enabled = false;
yield return new WaitForSeconds(takendamage);
renderer.enabled = true;
yield return new WaitForSeconds(takendamage);
renderer.enabled = false;
yield return new WaitForSeconds(takendamage);
renderer.enabled = true;
yield return new WaitForSeconds(takendamage);
renderer.enabled = false;
yield return new WaitForSeconds(takendamage);
renderer.enabled = true;
yield return new WaitForSeconds(takendamage);
}
Don’t understand, I’m pretty sorry, I’m beginning coding…
What is the problem, specifically? Can’t find the renderer? Use gameObject.GetComponent().enabled… where is the component you want to get and gameObject directs it to the go that this script is on.
Also, use code tags.