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 Pixelated_Lagg · May 03, 2021 at 11:05 PM · integer

Integer turning to zero when I don't want

I made a counter that adds 1 to an integer every 60 seconds. I want that integer value to be added to another integer to be displayed on the scene after the level. For some reason, the original integer value keeps equaling zero when I reach that scene, even when it previously was 1.

Heres my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using TMPro;
 using UnityEngine.SceneManagement;
 
 public class minutesscounter : MonoBehaviour
 {
     bool textupdate = true;
     TMP_Text text2 = null;
     public TMP_Text uiText2;
     private bool isVisible = true;
     TMP_Text text = null;
     public TMP_Text uiText;
     int minutes1 = 0;
     int minutes = 0;
 
     void Start()
     {
         text2 = uiText2.GetComponent<TMP_Text>();
         text = uiText.GetComponent<TMP_Text>();
         StartCoroutine(minutecounter());
     }
 
     IEnumerator minutecounter()
     {
         while (minutes < 100000) //Counts the minutes
         {
             yield return new WaitForSeconds(60);
             minutes = minutes + 1;
         }
     }
 
     void Update()
     {
         uiText.SetText(minutes + " minutes".ToString()); //Display on screen
         if (Input.GetKeyDown(KeyCode.N))
         {
             if (isVisible)
             {
                 HideUI(text);
                 isVisible = false;
             }
             else
             {
                 ShowUI(text);
                 isVisible = true;
             }
         }
 
         if (SceneManager.GetActiveScene () == SceneManager.GetSceneByName ("level1time")) //Hide this when the scene goes to "level1time"
         {
             if (Input.GetKeyDown(KeyCode.N))
             {
                 HideUI(text);
             }
             if (isVisible)
             {
                 HideUI(text);
             }
             if (textupdate == true)
             {
                 Debug.Log(minutes1);
                 minutes1 = minutes;
                 uiText2.SetText(minutes1 + " minutes".ToString());
                 textupdate = false;
             }
         }
     }
 
     public static void HideUI(TMP_Text text) //Hide the UI on command
     {
         text.transform.localScale = new Vector3(0, 0, 0);
     }
 
     public static void ShowUI(TMP_Text text) //Re-show the UI on command
     {
         text.transform.localScale = new Vector3(1, 1, 1);
     }
 }


Any help is greatly appreciated :)

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
Best Answer

Answer by sSteinmann · May 05, 2021 at 06:54 AM

Have you set your GameObject as DontDestroyOnLoad which has the minutesscounter script on it? If not it will be destroyed during loading (if you unload the scene it's in) and so the counted minutes would be lost. You could also mark the minutes variable as static.


But why are you counting the minutes in a coroutine? You could simply save Time.time (float) on start and then at the display subtract the current time by the saved time, then you have the elapsed time in seconds, so you just have to divide it by 60 to get the minutes (you could also cast it to int if you don't care about partial minutes): elapsedMinutes = int(savedStartTime - Time.time)


You also don't need to check the scene name in update, you could subscribe to SceneManager.sceneLoaded or SceneManager.activeSceneChanged to get called when a scene is loaded or changed

Comment
Add comment · Show 3 · 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 Pixelated_Lagg · May 10, 2021 at 08:05 PM 0
Share

@sSteinmann So turning the int $$anonymous$$utes to static fixed the problem of it not updating the text on the next scene. For some reason it now only adds 2 to $$anonymous$$utes every 2 $$anonymous$$utes instead of 1 to $$anonymous$$utes per $$anonymous$$ute.

avatar image Bunny83 Pixelated_Lagg · May 10, 2021 at 08:11 PM 0
Share

That most likely means you have attached your script two times to some gameobject in your scene. This probably was your issue in the first place. So you may have referenced the wrong version originally? That's hatd to tell from what we know about your project.

avatar image Pixelated_Lagg · May 10, 2021 at 08:15 PM 0
Share

Never$$anonymous$$d. The reason it would add more than necessary to the int was because I had multiple instances of the script running.

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

118 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

Related Questions

Dealing with Texture2D.width as it is a float number 3 Answers

Why is there sometimes an f behinf a number? 3 Answers

Int List won't let me add an Int Variable 1 Answer

How to increase an integer by 1 every 2 of another integer 1 Answer

How do I insert a variable into a URL? Please Help! 1 Answer


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