• 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 /
This question was closed Oct 09, 2013 at 04:40 PM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by ThePositiveMoxie · Oct 09, 2013 at 03:13 PM · value

Certain Variables Never Update

Hello, I've run into an issue. I created a timer that records the amount of time offline and give you the time it is after logged in, in order to gain up to the max amount of lives while your on or off the game. Certain variables in my scripts won't update. This is the most important one right now. So I am posting this one here. You will see how a Debug.Log exists when it's trying to see if it'll reach below zero. It actually fires just fine, but you will notice the staticVariables.remainingLives += 1 never updates in the other script. This happens to me when updating a position of a character and camera in FixedUpdate as well. Hoping someone can help. THANKS!

 using UnityEngine;
 using System.Collections;
 using System;
 public class LivesTimer : MonoBehaviour {
 
     public StaticVariables staticVariables;
     public OTTextSprite livesLeftOT;
     public OTTextSprite livesTimerText;
     public TimeSpan timeDifference;
     public DateTime currentDate;
     public DateTime oldDate;
     public long temp;
     public int maxTime;
     public DateTime timeMax = new DateTime(2013, 1, 1, 0, 2, 0);
     public DateTime timeDefault = new DateTime(2013, 1, 1, 0, 2, 0);
     public int dividedTimeMinutes;
     public int dividedTimeSeconds;
     public int offSeconds;
     public int offMinutes;
     public int timeDiffOffSec;
     public static int livesGained;
     public int newMinutesLeft;
     public int newSecondsLeft;
     public int differenceMinRemain;
 
 void Start(){
     
     // Check if Lives exists, if so update remainingLives
     if (PlayerPrefs.HasKey("Lives"))
     {
         staticVariables.remainingLives = PlayerPrefs.GetInt("Lives");
         
         
     }
     else
     {
         // if Lives doesn't exist, give it a default of 5
         staticVariables.remainingLives = 5;
     }
     
     if (PlayerPrefs.HasKey("sysString")){
         // Record all timeData
         currentDate = DateTime.Now;
         
         temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
         oldDate = DateTime.FromBinary(temp);
         
         timeDifference = currentDate.Subtract(oldDate);
         Debug.Log("Difference: " + timeDifference.ToString());
         
         Debug.Log("Current: " + currentDate.ToString());
         Debug.Log("Old: " + oldDate.ToString());
         
         // dividedTimeMinutes = Mathf.Floor(timeDifference.get_Minutes() / maxTime);
         
         
         Debug.Log("DividedTime: " + dividedTimeMinutes.ToString());
     }
     
     Debug.Log("timeDifference: " + timeDifference.ToString());
     
     livesGained = Convert.ToInt32(Mathf.Floor(timeDifference.Minutes / maxTime));
     Debug.Log("livesGained: " + livesGained.ToString());
     
     
     
     differenceMinRemain = Convert.ToInt32(Mathf.Floor(timeDifference.Minutes % maxTime));
     Debug.Log("differenceMinRemain: " + differenceMinRemain.ToString());
     
     newMinutesLeft = PlayerPrefs.GetInt("minutesLeft") - differenceMinRemain;
     Debug.Log("newMinutesLeft: " + newMinutesLeft.ToString());
     
     if (timeDifference.Seconds > PlayerPrefs.GetInt("secondsLeft")){
     newSecondsLeft = timeDifference.Seconds - PlayerPrefs.GetInt("secondsLeft");
     newMinutesLeft -= 1;
     
         if (newMinutesLeft < 0){
         // THE ISSUE LIES HERE!!
             livesGained += 1;
             staticVariables.remainingLives +=1;
             Debug.Log("checkIfRan #1");
             livesLeftOT.text = staticVariables.remainingLives.ToString();
             newMinutesLeft = maxTime - 1;
             
         }
     
     newSecondsLeft = 60 - newSecondsLeft;
     }
     
     if (timeDifference.Seconds < PlayerPrefs.GetInt("secondsLeft")){
     newSecondsLeft = PlayerPrefs.GetInt("secondsLeft") - timeDifference.Seconds;
     
         if (newMinutesLeft < 0){
         // THE ISSUE LIES HERE!!
             Debug.Log("checkIfRan #2");
             livesGained += 1;
             staticVariables.remainingLives +=1;
             livesLeftOT.text = staticVariables.remainingLives.ToString();
             newMinutesLeft = maxTime - 1;
             
             
         }
     }
     
      
         
         if (staticVariables.remainingLives > 5){
             staticVariables.remainingLives = 5;
             timeMax = timeDefault;
         }
     
     timeMax = new DateTime(2013,1,1,0,newMinutesLeft, newSecondsLeft);
     
     livesLeftOT.text = staticVariables.remainingLives.ToString();
     
     
     livesTimerText.text = newMinutesLeft.ToString() + " : " + newSecondsLeft.ToString();
     
 
     StartCoroutine("OneSecondUpdate");
 }
Comment
Add comment · Show 2
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 Inkinsarto · Oct 09, 2013 at 04:07 PM 0
Share

This might sound a little strange, but have you tried to change the variables to private?

avatar image ThePositiveMoxie · Oct 09, 2013 at 04:18 PM 0
Share

Yeah, just did. Can't change remainingLives though, since it's in another script. I've never had this issue, so kind of baffles me.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by ThePositiveMoxie · Oct 09, 2013 at 04:40 PM

I guess I figured it out, but not sure about in my other scripts. Seems to be because I put it all in the start function. Apparently the other script may not be fully initialized yet? All I know is I had to stick it in a Coroutine, with a WaitForSeconds set really low. Then it made all the changes I asked for. Not sure if there is any other reasons, but works for me.

Comment
Add comment · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

what is wrong with my GUI script? 1 Answer

Timer and TextMesh issue.... 1 Answer

value float approximately equal to float 2 Answers

Adding array values together 3 Answers

How is this always the same outcome?? Caution: contains sex! ;) 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