Search Unity

Button not working

Discussion in '2D' started by Laurenz_02, Apr 24, 2019.

  1. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    Hello, I am currently working on my first game in unity.
    I have made it so, that when your lose after x collisions, you are taken to a restart scene, where there is a restart button, which is supposed to bring you back to the actual game.

    The way I did this was:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class RestartController : MonoBehaviour
    {
    public void RestartControl()
    {
    SceneManager.LoadScene ("SampleScene");
    }
    }

    I added this script to my restart button object and then dragged the button into the onclick event and fixed the stuff there.

    When I have x collisions, I am being taken to the restarting scene, so that works. But when I click on the restart button, I quickly see the actual game scene, but then it immediately flashes back to the restarting scene.

    Can anyone help me out please?
    Thanks already!
     
  2. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    You probably forgot to reset the variable that counts collisions. You go to the original scene, which immediately notices that you have too many collisions and sends you back.

    It also means you're probably checking collision count in `Update`. If so, it would be more efficient to do it when the collision occurs.
     
    Laurenz_02 likes this.
  3. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    Thank you SO MUCH MAN!!
    I have been looking everywhere on the internet, but couldn't find the solution. Didn't know it was that simple.

    Really appreciate your reply man!!
     
  4. zioth

    zioth

    Joined:
    Jan 9, 2019
    Posts:
    111
    Happy to help!
     
    Laurenz_02 likes this.