Change sprite of player when collision

Hi !

In my game when my player hit an ennemy i want that the game pause and wait 3 seconds before going to the menu.

But the problem is that the game load the menu (without waiting 3 seconds). Moreover, the sprite of my player is not changed.

What’s wrong with my script ? I can’t find the problem.

Thanks and “passez une bonne journée” !

using UnityEngine;
using System.Collections;

public class OnCollision : MonoBehaviour {
    public Sprite sprite2;
    public SpriteRenderer spriteRenderer;   
    public AudioSource[] sounds;
    public AudioSource hit;
    void OnCollisionEnter2D(Collision2D whatHitMe)
    {
        if(whatHitMe.gameObject.tag == "Enemy")
        {
            hit.Play();//Music play
            spriteRenderer.sprite = sprite2;// Change sprite of the player
            StartCoroutine(GoToTheMenu());//Wait 1.5 seconds
            Time.timeScale =0;//Pause the game
            GetComponent<game>().GameOver();//Load Game over (= go to the menu)   
        }
    }
    IEnumerator GoToTheMenu()
    {
        yield return new WaitForSeconds(1.5f);
       

    }
    void Start(){
        hit = sounds[1];
    }
}

Move the GetConponent().GameOver(); into the Coroutine after the yield , that way it will fire after the wait time. I also suggest starting it last and moving the timescale above it or into the function above the yield

Thanks ! But the problem is that my player don’t change his sprite2236411--149106--QUESTIONS.png

using UnityEngine;
using System.Collections;

public class OnCollision : MonoBehaviour {
    public Sprite sprite2;
    public SpriteRenderer spriteRenderer;   
    public AudioSource[] sounds;
    public AudioSource hit;
    void OnCollisionEnter2D(Collision2D whatHitMe)
    {
        if(whatHitMe.gameObject.tag == "Enemy")
        {
            hit.Play();
            Time.timeScale = 0;
            StartCoroutine(GoToTheMenu());
           
               
        }
    }
    IEnumerator GoToTheMenu()
    {
        spriteRenderer.sprite = sprite2; Debug.Log ("Done!");// Change sprite
        yield return new WaitForSeconds(1.5f);// Wait 1.5 seconds
        GetComponent<game>().GameOver();// Load Gameover()

    }
    void Start(){
        hit = sounds[1];
    }
}

Moreover if i keep Time.timeScale = 0 the Coroutine will never start.

I would probably avoid setting the time.timescale to 0 as you are trying to access things that need time. Taking it out should fix it, but if you really need to pause things around because they’re moving or something I suggest adding the void Update function and creating a bool for when the collision occurs.

  • collision occurs , set bool true
  • in update if(bool) run Coroutine , probably don’t even need that either you could use your own counter using time.deltatime but your choice
    Change the image , while waiting , after time run GameOver
private bool isDead   = false;
private bool spriteChanged = false;
private float time = 0.0f;
private float timeNeeded = 1.5f;

void Update()
{
       if(isDead)
       {
             
              if(!spriteChanged)
              {
                      //TODO: switch sprite here
                      spriteChanged = true;
              }
            
              time += Time.deltaTime;
              if(time >= timeNeeded)
              {
                      //TODO: Call function game over
              }
        }
}

Im on my phone so it’s a little hard to code hahah