Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Adding to another scripts variable after countdown

Discussion in 'Scripting' started by Wellamo, May 10, 2017.

  1. Wellamo

    Wellamo

    Joined:
    Nov 2, 2016
    Posts:
    13
    Hi all in need of some help please. im trying to add to a variable after a countdown, i have the countdown working fine and but at the end of it nothing happens.Here is my script. It is changing the value of a variable from another script called lifetime.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System;
    6.  
    7. public class tapTimePay : LifeTime {
    8.  
    9.     public float JobTime;
    10.      
    11.     void FixedUpdate () {
    12.      
    13.         if (JobTime > 0) {
    14.             JobTime -= Time.deltaTime;
    15.             Debug.Log ("" + JobTime);
    16.         }
    17.         else {
    18.          
    19.             enabled = false;
    20.             addTime ();
    21.  
    22.  
    23.         }
    24.     }
    25.     void addTime(){
    26.      
    27.         span1.Add (TimeSpan.FromMinutes (5));
    28.         minnutes = span1.Minutes;
    29.     }
    30. }
    Many thanks for reading this i hope somebody can spot the problem . Thankyou
     
    Last edited: May 10, 2017
  2. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    you're setting enabled = false, which disables your component. This causes it to no longer receive the FixedUpdate() event.
    Also, please use the code tags next time, they are the 4th button from the right in the menu bar of this editor.
     
  3. Wellamo

    Wellamo

    Joined:
    Nov 2, 2016
    Posts:
    13
    Hi thankyou for the reply and ill remember to do that in future. Still getting used to this. I tryed your suggestion but it still does not update the value on countdown.Ill put up the other script in case its that making problems.Thankyou

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using System;
    7.  
    8. public class LifeTime :MonoBehaviour {
    9.    
    10.     public Text Days;
    11.     public Text Hours;
    12.     public Text Minutes;
    13.     public Text Seconds;
    14.  
    15.  
    16.     public static int days =1;
    17.     public static int hours=2;
    18.     public static int minnutes=3;
    19.     public static int seconds=4;
    20.  
    21.     public TimeSpan span1=new TimeSpan(days,hours,minnutes,seconds);
    22.  
    23.  
    24.     void FixedUpdate(){
    25.        
    26.         span1=span1.Subtract (TimeSpan.FromSeconds(Time.deltaTime));
    27.  
    28.          seconds = span1.Seconds;
    29.          minnutes = span1.Minutes;
    30.          hours = span1.Hours;
    31.          days = span1.Days;
    32.  
    33.         Days.text = ("Days ") + days;
    34.         Hours.text = ("Hours ") + hours;
    35.         Minutes.text = ("Minutes ") + minnutes;
    36.         Seconds.text = ("Seconds ") + seconds;
    37.     }
    38. }
    k
     
  4. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    Please talk me through what exactly you're expecting. Which variable should change when and how? Please also edit your first post with code tags so it's more readable.
    Please also note that your first class extends your second. This means it will override functions of the same name. So per instance it will only call FixedUpdate once.
     
  5. Wellamo

    Wellamo

    Joined:
    Nov 2, 2016
    Posts:
    13
    Ok I have a ui displaying the days,hours,minutes,seconds of a timespan(span1).
    After jobtime has counted down to 0 i want to increase the minutes on the timespan .
    So that it also changes the ui value shown.
    Maybe your point that im overriding the fixed update is the problem.
    or should i be trying to change the var minutes instead of adding to the timespan?
    Thankyou for your patience i do appreciate
     
  6. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    Ok I didn't use TimeSpan before so I didn't realize right away : TimeSpan is a struct ! This means it's passed by Value not Reference. A common pattern on structs is to pass results from operations as a new struct, since it doesn't cost much performance.
    So my point is, TimeSpan.Add() does not add anything to your current TimeSpan. Instead it creates a new TimeSpan with the desired value. So you want to write span1 = span1.Add (TimeSpan.FromMinutes (5));
     
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Here's a simply countdown time I made for another thread which might help;

    Code (CSharp):
    1.     using System;
    2.     using UnityEngine;
    3.    
    4.     public class Countdown : MonoBehaviour
    5.     {
    6.         public float CountdownTimerInSeconds = 30f;
    7.         public float resetTimerToXSeconds = 50f;
    8.    
    9.         public void Update()
    10.         {
    11.             if(Input.GetKeyDown(KeyCode.Space))
    12.             {
    13.                 ResetTimer(resetTimerToXSeconds);
    14.             }
    15.             CountdownTimerInSeconds -= Time.deltaTime;
    16.         }
    17.    
    18.         private void OnGUI()
    19.         {
    20.             GUI.Label(new Rect(10, 10, 150, 50), FloatToTime(CountdownTimerInSeconds));
    21.         }
    22.    
    23.         private void ResetTimer(float newTimeInSeconds)
    24.         {
    25.             CountdownTimerInSeconds = newTimeInSeconds;
    26.         }
    27.    
    28.         private string FloatToTime(float seconds)
    29.         {
    30.             var span = TimeSpan.FromSeconds(seconds);
    31.             return string.Format("{0}:{1:00}:{2:00}", span.Minutes, span.Seconds, span.Milliseconds);  
    32.         }
    33.     }
    34.  
     
    Wellamo likes this.
  8. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class tapTimePay : MonoBehaviour { // BUG : Merged both classes into one
    6.  
    7.     public Text Days;
    8.     public Text Hours;
    9.     public Text Minutes;
    10.     public Text Seconds;
    11.  
    12.  
    13.     public static int days =1;
    14.     public static int hours=2;
    15.     public static int minnutes=3;
    16.     public static int seconds=4;
    17.  
    18.     public TimeSpan span1 = new TimeSpan(0,0,0,4);
    19.  
    20.     void FixedUpdate () {
    21.  
    22.         if (span1.CompareTo(new TimeSpan(0,0,0,0,0)) > 0) // BUG : This is a comparer : It returns 0 if equal, -1 if less, +1 if more
    23.         {
    24.             span1 = span1.Subtract(TimeSpan.FromSeconds(Time.deltaTime)); // BUG replaced with span1.Subtract
    25.             Debug.Log (span1);
    26.         }
    27.         else // BUG : Removed enabled = false
    28.             addTime ();
    29.  
    30.  
    31.         seconds = span1.Seconds;
    32.         minnutes = span1.Minutes;
    33.         hours = span1.Hours;
    34.         days = span1.Days;
    35.  
    36.         Days.text = ("Days ") + days;
    37.         Hours.text = ("Hours ") + hours;
    38.         Minutes.text = ("Minutes ") + minnutes;
    39.         Seconds.text = ("Seconds ") + seconds;
    40.  
    41.     }
    42.     void addTime(){
    43.  
    44.         span1 = span1.Add (TimeSpan.FromMinutes (5)); // BUG : span1 = ... since Add returns the result
    45.         // minnutes = span1.Minutes; // BUG : What is this for?
    46.     }
    47. }
    PS : BUG just meant for the highlighting, most are just plain old comments :)
     
    Wellamo likes this.
  9. Wellamo

    Wellamo

    Joined:
    Nov 2, 2016
    Posts:
    13
    Thankyou very much for this its a great help and much cleaner. I appreciate your time