Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 17, 2018 at 10:04 PM by polan31 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by polan31 · Apr 05, 2018 at 08:42 AM · collisionspriteload

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
1
Best 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;
         }
     }
 }
 
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image polan31 · Apr 05, 2018 at 02:44 PM 0
Share

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. :)

avatar image polan31 · Apr 05, 2018 at 03:51 PM 0
Share

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

avatar image Koyemsi polan31 · Apr 05, 2018 at 04:25 PM 0
Share

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 ?

avatar image polan31 Koyemsi · Apr 05, 2018 at 04:35 PM 0
Share

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 :) :) :)

Show more comments

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision detection not working properly with 2D sprites 1 Answer

Controller Collision Load Scene 2 Answers

Polygon Collider 2D Not Precise Enough 0 Answers

Best way to change sprite in Image (GUI 4.6) ? 1 Answer

help with script load level on 0 enemies 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges