I’m a newbie in Unity (though I have experience in C#), so I’m following some tutorials and making a few modifications to learn more.
I have this script, that makes a Text component fade away:
public class FadeAway : MonoBehaviour {
[SerializeField] private Text _textUI;
private CountdownTimer countdownTimer;
private int fadeDuration = 5;
private bool fading = false;
// Use this for initialization
void Start () {
countdownTimer = GetComponent<CountdownTimer>();
StartFading(fadeDuration);
}
// Update is called once per frame
void Update () {
if (fading){
float alphaRemaining = countdownTimer.GetProportionTimeRemaining();
print(alphaRemaining);
Color color = _textUI.material.color;
color.a = alphaRemaining;
_textUI.material.color = color;
if (alphaRemaining < 0.01)
fading = false;
}
}
private void StartFading(int timerTotal){
countdownTimer.ResetTimer(timerTotal);
fading = true;
}
}
And also, this class that was given by the tutorial (I haven’t modded it) to act as a countdown timer:
public class CountdownTimer : MonoBehaviour
{
private float countdownTimerStartTime;
private int countdownTimerDuration;
public int GetTotalSeconds(){
return countdownTimerDuration;
}
public void ResetTimer(int seconds){
countdownTimerStartTime = Time.time;
countdownTimerDuration = seconds;
}
public int GetSecondsRemaining(){
int elapsedSeconds = (int)(Time.time - countdownTimerStartTime);
int secondsLeft = (countdownTimerDuration - elapsedSeconds);
return secondsLeft;
}
public float GetFractionSecondsRemaining(){
float elapsedSeconds = (Time.time - countdownTimerStartTime);
float secondsLeft = (countdownTimerDuration - elapsedSeconds);
return secondsLeft;
}
public float GetProportionTimeRemaining(){
float proportionLeft = (float)GetFractionSecondsRemaining() / (float)GetTotalSeconds();
return proportionLeft;
}
}
Anyway, the problem is: I used [SerializeField] to get the reference to a single component on my canvas, the UI.Text I want the script to be applied to. On the Inspector, I assigned both scripts above to this UI.Text and passed the same UI.Text as a reference to the FadeAway script. But, if I add any other UI.Text on my canvas, it fades away too, even though I explicitly passed the single component I wanted to modify as a parameter to my script. What is happening? Could it be something I forgot? Hope this isn’t a stupid question.