Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SolisSaturnus · Mar 11, 2021 at 08:47 PM · scripting problemdropdown

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SolisSaturnus · Mar 11, 2021 at 09:10 PM 0
Share

How should I put the method into Start()? I am sorry I am knew to unity and C#

avatar image Omti1990 SolisSaturnus · Mar 16, 2021 at 04:39 PM 0
Share
 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

208 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges