Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Change sprite of player when collision

Discussion in 'Scripting' started by simonini_thomas, Aug 4, 2015.

  1. simonini_thomas

    simonini_thomas

    Joined:
    Apr 30, 2015
    Posts:
    33
    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" !

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class OnCollision : MonoBehaviour {
    6.     public Sprite sprite2;
    7.     public SpriteRenderer spriteRenderer;  
    8.     public AudioSource[] sounds;
    9.     public AudioSource hit;
    10.     void OnCollisionEnter2D(Collision2D whatHitMe)
    11.     {
    12.         if(whatHitMe.gameObject.tag == "Enemy")
    13.         {
    14.             hit.Play();//Music play
    15.             spriteRenderer.sprite = sprite2;// Change sprite of the player
    16.             StartCoroutine(GoToTheMenu());//Wait 1.5 seconds
    17.             Time.timeScale =0;//Pause the game
    18.             GetComponent<game>().GameOver();//Load Game over (= go to the menu)  
    19.         }
    20.     }
    21.     IEnumerator GoToTheMenu()
    22.     {
    23.         yield return new WaitForSeconds(1.5f);
    24.        
    25.  
    26.     }
    27.     void Start(){
    28.         hit = sounds[1];
    29.     }
    30. }
    31.  
     
  2. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    Move the GetConponent<game>().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
     
  3. simonini_thomas

    simonini_thomas

    Joined:
    Apr 30, 2015
    Posts:
    33
    Thanks ! But the problem is that my player don't change his sprite QUESTIONS.png

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class OnCollision : MonoBehaviour {
    5.     public Sprite sprite2;
    6.     public SpriteRenderer spriteRenderer;  
    7.     public AudioSource[] sounds;
    8.     public AudioSource hit;
    9.     void OnCollisionEnter2D(Collision2D whatHitMe)
    10.     {
    11.         if(whatHitMe.gameObject.tag == "Enemy")
    12.         {
    13.             hit.Play();
    14.             Time.timeScale = 0;
    15.             StartCoroutine(GoToTheMenu());
    16.            
    17.                
    18.         }
    19.     }
    20.     IEnumerator GoToTheMenu()
    21.     {
    22.         spriteRenderer.sprite = sprite2; Debug.Log ("Done!");// Change sprite
    23.         yield return new WaitForSeconds(1.5f);// Wait 1.5 seconds
    24.         GetComponent<game>().GameOver();// Load Gameover()
    25.  
    26.     }
    27.     void Start(){
    28.         hit = sounds[1];
    29.     }
    30. }
    31.  
    Moreover if i keep Time.timeScale = 0 the Coroutine will never start.
     
  4. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    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

    Code (CSharp):
    1. private bool isDead   = false;
    2. private bool spriteChanged = false;
    3. private float time = 0.0f;
    4. private float timeNeeded = 1.5f;
    5.  
    6. void Update()
    7. {
    8.        if(isDead)
    9.        {
    10.              
    11.               if(!spriteChanged)
    12.               {
    13.                       //TODO: switch sprite here
    14.                       spriteChanged = true;
    15.               }
    16.            
    17.               time += Time.deltaTime;
    18.               if(time >= timeNeeded)
    19.               {
    20.                       //TODO: Call function game over
    21.               }
    22.         }
    23. }
    24.  
    Im on my phone so it's a little hard to code hahah