Since I collect you are a brand new learner, and this is likely to be associated to a faculty project, I will chorus from sharing a whole script to copy-paste, and as an alternative stroll you thru some classes you possibly can apply to enhance your individual code:
You are utilizing GetComponent unsuitable.
foo = GetComponent
says “overwrite the reference in my variable foo
with the first part of kind X
you discover on this object”
Which means that is ineffective: BACK = GetComponent
, since GameObject
just isn’t a kind of Element
. All you have executed is made your self an alias for the built-in property gameObject
, that means “the item this script occasion is connected to”. We needn’t make our personal variable for this.
It additionally implies that these two traces can’t presumably do what you need:
_seconds = GetComponent();
_minuts = GetComponent();
Since GetComponent
solely ever returns the first matching part it finds, it will assign that first picture part to each variables, so that they’re each pointing on the identical Picture. Not what you need.
Greater up, you declared these as public
variables:
public Picture _seconds;
public Picture _minuts;
So it seems such as you wished to set these by hand within the inspector window. These calls to GetComponent
are overwriting something you set within the inspector although. Delete the GetComponent
calls, and you’ll configure this within the inspector as you’d deliberate.
(Additionally, it is standard to make use of the underscore _
prefix for personal variables. Do not use it for public
variables, or you might confuse people attempting to learn your code).
WaitForSeconds just isn’t an correct strategy to construct a clock.
While you pause a coroutine with yield return new WaitForSeconds(someNumber);
, your coroutine will resume the body after that quantity of seconds has handed.
That implies that the precise time elapsed will probably be someNumber + delta
the place delta is principally a random quantity between zero and Time.deltaTime
(the quantity of sport time elapsed because the begin of the earlier body).
That is not a giant deal once we’re utilizing WaitForSeconds
for a one-off occasion, like a capability cooldown. If I disable a participant’s particular transfer for five seconds, and it is disabled till 5.01 seconds have handed, they will not discover – the sport could not have reacted to their button press attempting to make use of the flexibility any sooner than this body anyway.
However once we chain these waits one after one other, the error within the first will get added to the following, which snowballs to the following…
So if you happen to ran two copies of the sport with this code, their clocks would progressively drift out of sync, and if the sport have been operating at a decrease framerate, the in-game clock would run slower.
Worse, because you’re utilizing two separate coroutines to control those and tens digits, these coroutines can exit of sync with one another. As soon as sufficient drift accumulates, your clock begins counting wonkily, like:
…05, 06, 07, 08, 18, 19, 10, 11…
As an alternative, we must always use a clock for a clock, after which get our digits from that.
-
Take a time supply like
Time.timeAsDouble
-
Subtract the beginning time of your clock (which you save in a variable whenever you need to begin counting up from zero)
-
Divide it by the “slowness” of your clock. On this instance, it seems such as you need one clock second to final two sport seconds, so that you’d divide by two (or, equivalently, multiply by 0.5).
-
Solid the outcome to an
int
(this implicitly rounds down for optimistic numbers) -
Now you will have the overall variety of clock seconds elapsed because the begin of counting.
-
If that is higher than it was on the finish of the final body, increment your final digit.
-
If this digit wraps-around, increment the following digit. Repeat till you run out of digits, otherwise you get an increment with out wrapping.
While you begin naming variables “one, two, three…”, what you actually need is an array or Record
.
Writing public Sprite[] numerals;
offers you an array of sprites you possibly can assign within the inspector. Put zero within the high slot (since C# arrays begin at index 0), one within the subsequent slot, and many others.
This fashion, you needn’t micro-manage your clock in code, telling it that after “zero” it ought to present “one”, and after that it ought to present “two”… As an alternative, you possibly can inform it to indicate numerals[value]
. Must go to the following digit? Simply add one to the index worth
(++
). Computer systems are good at math, so that they already know how one can go from 0
to 1
to 2
with out you spelling out each step of that sequence in code.
We will take this additional, and put our Picture
elements representing our show digits into an array too, exploiting the sequence of the array to automate carries, the place a low digit wraps round and the following larger digit must increment.
First, let’s encapsulate the behaviour we want from every digit of our clock readout in its personal little knowledge construction. The Serializable
attribute lets us see and edit the information inside utilizing the Unity the inspector.
[System.Serializable]
public class Digit {
public Picture picture; // Assign in inspector.
public int max;
public int worth { get; personal set; }
public Digit(int max) {
this.max = max;
}
public bool IncrementAndCarry(Sprite[] numerals) {
bool shouldCarry = false;
if (++worth > max) {
worth = 0;
shouldCarry = true;
}
picture.sprite = numerals[value];
return shouldCarry;
}
}
Then we will outline our clock face as an array of those digits (from least to most vital):
// Default to MM:SS format, however you possibly can change this within the inspector.
public Digit[] digits = new[] {
new Digit(9), // Single seconds, 0-9 (after 9, we supply)
new Digit(5), // Tens of seconds, 0-5 (after 59, we supply)
new Digit(9), // Single minutes, 0-9 (after 9:59, we supply)
new Digit(9) // Tens of minutes, 0-9 (after 99:59, we wrap)
};
Now, as soon as we detect a brand new second is upon us, we will name:
personal void NextSecond() {
for (int i = 0; i < digits.Size; i++) {
if (!digits[i].IncrementAndCarry(numerals)) {
break; // Abort if we needn't carry additional.
}
}
}
There we go. A lot shorter code, and this code can scale up from a single-digit show to any variety of digits, in any counting base you want. That generalization and brevity is the facility we get from abstractions like arrays and customized lessons.