- Home /
Why does my MatchTime Class not work when using Properties?
I've been trying to figure out why my public property is not updating when referenced to on another script.
MatchTime.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MatchTime : MonoBehaviour{
private int _matchOverTime;
private int _matchBeginTime;
private int _minutes;
private int _seconds;
private bool isRunning;
public MatchTime(int MatchBeginTime, int MatchOverMinutes)
{
_matchOverTime = MatchOverMinutes;
_matchBeginTime = MatchBeginTime;
}
public int Minutes { get { return _minutes; } private set { _minutes += (int)Mathf.FloorToInt(Time.deltaTime / 60f); } }
public int Seconds { get { return _seconds; } private set { _seconds += (int)Mathf.FloorToInt(Minutes * 60f); } }
}
Being Called Simply by, MatchHandler.cs (field is initialized in inspector)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MatchHandler : MonoBehaviour {
public MatchTime matchTime;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log(string.Format("{0}:{1}", matchTime.Minutes, matchTime.Seconds));
}
}
Answer by Bunny83 · Sep 09, 2017 at 09:55 AM
Are you sure you understand what properties are about? Have a closer look at one of your properties:
public int Minutes
{
get { return _minutes; }
private set { _minutes += (int)Mathf.FloorToInt(Time.deltaTime / 60f); }
}
So whenever you access (read) your Minutes you just read the backing field "_minutes". However whenever you "set" / assign (write) something to your Minutes property you actually ignore the value that was assigned and instead you you try to increment the value based on your math formula.
This is actually counter intuitive and should never be done. If you do
Minutes = 5;
you expect that this "5" at least has some effect on Minutes which it doesn't in your case.
Further more your math it pointless. _minutes is an "int" value. Time.deltaTime is a float and is a fraction of a second. At a framerate of 60 fps deltaTime would be 0.016666 (== 1f / 60). You actually divide it further down so it becomes something like "0.0002777" You then round down the number to the nearest integer which is always just "0". The int cast is also redundant as FloorToInt already returns an integer.
In the end no matter what you do, setting "something" to the Minutes property does not change anything because _minutes += 0;
doesn't change anything.
If you just want to track the passed time you may want to do something like this:
float _time = 0;
public int MilliSeconds
{
get { return Mathf.FloorToInt((_time*1000) % 1000);} // 0 - 999
}
public int Seconds
{
get { return Mathf.FloorToInt((_time) % 60);} // 0 - 59
}
public int Minutes
{
get { return Mathf.FloorToInt((_time/60) % 60);} // 0 - 59
}
public int Hours
{
get { return Mathf.FloorToInt((_time/(60*60)) % 24);} // 0 - 23
}
public int Days
{
get { return Mathf.FloorToInt((_time/(60*60*24)));} // 0 - ...
}
_time would represent your actual time in seconds. It needs to be a float in order to be able to update it every frame. To increase the time you would just do
_time += Time.deltaTime;
Finally you have a conceptional error here. It seems you want to create an instance of that class using your constructor. In this case you must not derive your class from MonoBehaviour. MonoBehaviours are components which must be attached to a gameobject. They can only be created with AddComponent and can't be created with "new".
It's not really clear if this class should actually actively track the match time during the match or if it's just used for displaying highscores.
Your answer
Follow this Question
Related Questions
countdown to persist through scenes 2 Answers
Timer doesn't work properly 2 Answers
Slider to modify a countdown time in another scene ? 1 Answer
timer not ticking down 2 Answers
How to make this display milliseconds? 3 Answers