- Home /
The question is answered, right answer was accepted
How to make a different sprite load after every hit?
I have ten sprites assigned to one gameObject.
They all charge after a collision with enemy.
How to make a different sprite load after every hit?
It should look like this:
First hit - sprite 1 changes into sprite 2 and after 2 seconds into sprite 3 ;
Second hit - sprite 3 changes into sprite 4 and after 2 seconds into 5;
Fird hit - sprite 5 changes into sprite 6 and after 2 seconds into 7;
Fourth hit - sprite 7 changes into sprite 8 and after 2 seconds into 9;
To change the sprite 1 in the sprite 2 and after 2 seconds into sprite 3 I use IEnumerator.
I know it's a simple thing, but I can only load all at once.
I will be grateful for every answer :)
Answer by Koyemsi · Apr 05, 2018 at 10:53 AM
Put this code on the game object that has the sprite you wish to change. Don't forget to feed the mySprites[] array in the inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TempSprite : MonoBehaviour {
private SpriteRenderer mySpriteRenderer;
public Sprite[] mySprites;
private int currentSpriteId = 0;
void Start () {
mySpriteRenderer = GetComponent<SpriteRenderer> ();
assignSprite ();
}
void OnCollisionEnter2D () {
StartCoroutine (nextSprite (0));
}
void assignSprite () {
mySpriteRenderer.sprite = mySprites [currentSpriteId];
}
IEnumerator nextSprite (float waitTime) {
yield return new WaitForSeconds (waitTime);
currentSpriteId++;
assignSprite ();
switch (currentSpriteId) {
case 2:
StartCoroutine (nextSprite (2.0f));
break;
case 4:
StartCoroutine (nextSprite (2.0f));
break;
case 6:
StartCoroutine (nextSprite (2.0f));
break;
}
}
}
Hey, thank you very much for the answer.
I really appreciate it.
Unfortunately, the script is not working properly.
After hitting the first sprite (SPRITE1) object changes very quickly into the SPRITE2 and after the same moment into the SPRITE3 (which is great and it should look like this). But after a few moments, without being hit, it changes into a SPRITE4 and then will stop.
After the second hit it turns into SPRITE5 and after a short moment into SPRITE6.
It work well, but I wanted it to stop on SPRITE3 and change in SPRITE4 and (after 2s) into SPRITE5 only after the second hit.
You know, it should looks like:
SPRITE1 (hit)-SPRITE2 AND AFTER 2 SECONDS SPRITE 3 (THEN STOP)
SECOND HIT- CHANGE ON SPRITE 4 AND AFTER 2 SECONDS INTO SPRITE 5 (THEN STOP) etc.
I can't use this method, but I will try to do something.
Thank you again, It was a quick and helpful answer. :)
Ok, I've changed your code (a small change), now it looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TempSprite : $$anonymous$$onoBehaviour {
private SpriteRenderer mySpriteRenderer;
public Sprite[] mySprites;
private int currentSpriteId = 0;
void Start () {
mySpriteRenderer = GetComponent<SpriteRenderer> ();
assignSprite ();
}
void OnCollisionEnter2D () {
StartCoroutine (nextSprite (0));
}
void assignSprite () {
mySpriteRenderer.sprite = mySprites [currentSpriteId];
}
IEnumerator nextSprite (float waitTime) {
yield return new WaitForSeconds (waitTime);
currentSpriteId++;
assignSprite ();
switch (currentSpriteId) {
case 0:
StartCoroutine (nextSprite (0f));
break;
case 3:
StartCoroutine (nextSprite (0f));
break;
case 5:
StartCoroutine (nextSprite (0f));
break;
case 7:
StartCoroutine (nextSprite (0f));
break;
}
}
}
And now it works perfectly.
Thanks again.
Best wishes
You are welcome, really glad I could help. If I understand well, my error concerned the switch statement, when evaluating currentSpriteId, so it was easy to fix. But I notice that you now call the coroutine with a zero parameter, so you don't have your 2 seconds latency anymore, do you ?
Yes, unfortunately I do not have two seconds.
I'm still working on it, but it can not be different.
When you set it above zero, the first sprite changes into the second very quickly, but the next one - very slow in another : /
But if you know how to solve it, I will not get angry if you write :) :) :)