Search code examples
c#androidunity-game-enginevirtual-realitygoogle-vr

Script for setting CountDown timer and Canvas does not play properly in Unity


I'm building a first person VR shooting game and the script responsible for CountDown and displaying Canvas on which Play Again Button appears does not play..

Here is the screen of the game play and the script for better understanding:

GamePlay

`

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

public class playGameAgainScript : MonoBehaviour {

    //Use this for Timer
    public Text countDown;
    private float timer = 60;

    //Declare Button
    public Button playAgainButton;

    void Start() {
        //Set countDown value
        countDown = GetComponent<Text>();
    }

    void Update() {
        timer -= Time.deltaTime;
        countDown.text = "" + timer.ToString ("f0");
        if (timer <= 0) {
            timer = 0;
            playAgainButton.gameObject.SetActive (true);
        } 
    }

    public void PlayAgain() {
        UnityEngine.SceneManagement.SceneManager.LoadScene ("Main Scene");
    }
}

`


Solution

  • The problem was that the playGameAgainButton was set to inactive as default and therefore the editor had nothing to look for.. And by moving the functionality from this script to a different place, it solved the probblem.. I moved it to the main playerScript and it works fine.