- Home /
Why is my dropdown not able to control my game countdown time?
System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Timer : MonoBehaviour { public static float timeMax = StartTime;
public bool timerIsRunning = false;
public Text timeText;
public Dropdown DropdownTimer;
public bool pick = false;
public static float StartTime;
private float PickTime;
public bool check = false;
public void Start()
{
// Starts the timer automatically
timerIsRunning = true;
}
public void Play()
{
if (check == false)
{
StartTime = 60;
}
else
{
StartTime = PickTime;
}
}
public void Drop()
{
switch (DropdownTimer.value)
{
default:
PickTime = 60;
check = true;
break;
case 1:
PickTime = 30;
check = true;
break;
case 2:
PickTime = 45;
check = true;
break;
case 3:
PickTime = 60;
check = true;
break;
}
}
public void Update()
{
timeText.text = ("Time: " + PickTime.ToString());
if (timeMax == 0)
{
timeMax = PickTime;
}
if (timerIsRunning)
{
if (timeMax > 0)
{
timeMax -= Time.deltaTime;
DisplayTime(timeMax);
}
else
{
Debug.Log("Time has run out!");
timeMax = 0;
timerIsRunning = false;
}
}
}
void DisplayTime(float timeToDisplay)
{
timeToDisplay += 1;
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
}
Answer by Omti1990 · Mar 11, 2021 at 09:04 PM
Because you're not actually calling the Drop() method anywhere?
You either need to put the method into Start(), craft a custom inspector or use something like NaughtyAttributes or OdinInspector to call that method with a button from the inspector or whenever you change that DropdownTimer variable.
How should I put the method into Start()? I am sorry I am knew to unity and C#
public void Start()
{
**Drop()**
// Starts the timer automatically
timerIsRunning = true;
}
void Start(), void Awake(), void Update() and others are messages. That means Unity is going to execute those automatically if your script is a monobehaviour and certain conditions are fullfilled.
Start and Awake are called once when the GameObject is activated. Normally that is when you start the game or when you instantiate/spawn the object. But if you activate the GameObject later, the Start and Awake will only be called then.
Update is called every frame on an active GameObject/active monobehaviour. So the script will run every frame.
You also don't need to make Start, Awake, Update and others public, unity will also call those when they're private.
For the basics, I'd really recommend to watch the tutorials here: https://learn.unity.com/search?k=%5B%22q%3AScripting%22%5D
Your answer
Follow this Question
Related Questions
Populate a dropdown dynamically from an array of objects REST API response 0 Answers
Creating a dynamic dropdown menu based on active GameObjects 0 Answers
How Translation with a fils txt and script I18n ? 0 Answers
Get Dropdown Index value based on text value using Where c# filter 1 Answer
How can I make a Dropdown option to be converted on a text to appear somewhere? 0 Answers