I built a component named “Timer” having own Text UI component to keep information about time played. it starts as disabled. There is script attached to it as the following:
// your copy/pasted code hereusing System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro; // Required for TextMeshPro UI
public class TimerDisplay : MonoBehaviour
{
public TextMeshProUGUI timerText; // Drag your UI TextMeshPro element here in the Inspector
public GameObject gameOverPanel;
//public GameObject canvasTimer;
public AudioSource audioSource; // was private
private bool gameOverPlaying = false;
public float currentTime = 0; // time in seconds
private void Start()
{
timerText.enabled = true; //<------------
}
void Update()
{
if (currentTime > 0 && !gameOverPlaying)
{
currentTime -= Time.deltaTime; // decremenntinng val
// Format the time as minutes:seconds
string minutes = Mathf.Floor(currentTime / 60).ToString("00");
string seconds = (currentTime % 60).ToString("00");
timerText.text = minutes + ":" + seconds;
} else
{
//Debug.Log("debugging !!!");
if(!gameOverPlaying)
{
currentTime = 0;
Debug.Log("game over is not playing!!!");
gameOverPlaying=true;
GameOver();
}
}
}
public void GameOver()
{
gameOverPanel.SetActive(true);
Debug.Log("Game OVER <----------> ");
//gameOverPanel = .GetComponent<Panel>;
Time.timeScale = 0f; // blocca il gioco (opzionale)
audioSource = GetComponent<AudioSource>();
Debug.Log("Audio src:> ",audioSource) ;
// audioSource.clip = clip;
Cursor.lockState = CursorLockMode.None;
audioSource.Play();
}
}
The component is:
I
1 Like
the script wont run because the object is disabled.
1 Like
yes, because I don’t want it runs automatically from beginning, but when a button get clicked and the timer will appear end countdown will starts.
On the buttons following script is attached to it:
using UnityEngine;
public class HideStartingscreen : MonoBehaviour
{
public GameObject panel;
public Behaviour my_component;
private HideStartingscreen hideSript;
//private TimerDisplay timerViewing; // attebtionn here
public GameObject mainPlayer;
//public GameObject timerClock;
private TimerDisplay tmrDisplay;
public void __Hide()
{
//Debug.Log("To HIDE hiding screen !!!");
panel.SetActive(false);
gameObject.SetActive(false);
//hideSript = GetComponent<HideStartingscreen>();
//hideSript.enabled = true;
// timerDisplay.playClicked = true; //< -------------------------
// activate player
mainPlayer.SetActive(true);
// activate and viewing the TIMER
my_component.enabled = true;
Debug.Log("gmy componnent enabled = true");
//tmrDisplay.enabled = true;
//tmrDisplay = my_component.GetComponent<TimerDisplay>();
//Debug.Log("get TimerDisplay Component!XD XD XD XD!!");
///////timerClock.SetActive(true);
}
Furthermore I set its ‘on click’ event to run proper method:
I cannot get what I am missing because when I click on the button The timer doen’t appear on the screen…basically It is the text defined In the Timer defined component…
OK but this is a little chaotic here, and hard to follow
So you run the hide on click?
which sets a gameobject - i assume the button to disabled, stopping the code. potentially
hide then enables some stuff a panel and a timer.
what debugging did you do? is the timer counting down but you not seeing it on screen?
It looks to me as if the textMeshPro is enabled (which it prolly already was)… but nothing else is.
I could be missing it completely though, 5am first coffee, balls are NOT rollin yet. 
1 Like
what debugging did you do? is the timer counting down but you not seeing it on screen?
I run the game but I don’t see the timer on display, because It starts not activate but when clicking on the button It should be enabled: in the __Hide method:
public class HideStartingscreen : MonoBehaviour
{
public GameObject panel;
public Behaviour my_component;
private HideStartingscreen hideSript;
//private TimerDisplay timerViewing; // attebtionn here
public GameObject mainPlayer;
public void __Hide()
{
//Debug.Log("To HIDE hiding screen !!!");
panel.SetActive(false);
gameObject.SetActive(false);
mainPlayer.SetActive(true);
my_component.enabled = true; // activate and viewing the TIMER
Debug.Log("gmy componnent enabled = true");
}
}
but anyway I don’t see it after clicking the button
When you drag your “Timer” GameObject from the hierarchy in the inspector, it grabs the first component which derives from “Behaviour” which is the TMP UGUI component.
When you run this, you are enabling the TMP component and not the GameObject.
The issue here is that you are enabling the component, but the GameObject that the component is attached to is still disabled. Either use my_component.gameObject.SetActive(true) or change Behaviour my_component to GameObject my_component, reassign your timer and then use my_component.SetActive(true)
1 Like