Get single component from canvas, instead of all components
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.
Answer by tormentoarmagedoom · Aug 02, 2018 at 09:40 AM
Good day.
I don't see where you define the _textUI variable. I see you declare it, but, where define what object in the scene is?
If you are recieving the same effect on 2 texts is because you are not definig correctly what text you want to fade out, is probably you have something like "find child objects with Text component" to define it, so it finds 2 of them.
If want to have only 1 fade out, you need to define specificly what text is. for example, using transform.Find or making the variable public and assign it via scene inspector.
Bye!
Hi, thanks for replying! Isn't [SerializeField] another way to define variables, passing them as parameters from the inspector? (https://answers.unity.com/questions/1167757/how-to-get-a-singly-text-component-from-one-of-mul.html) I think it's just the same as making the variable public, as you said, except you can keep it private for good practices' sake.
If you are recieving the same effect on 2 texts is because you are not definig correctly what text you want to fade out, is probably you have something like "find child objects with Text component" to define it, so it finds 2 of them.
First, I was using GetComponent() to declare my _textUI variable. And when this happenned, I understood the same thing you mentioned, so I started using [SerializeField] to avoid it. This wasn't supposed to happen, was it?
mmm ok, I never has been $$anonymous$$ch about scripting, i learned all by myself and i dont know a mlot of things, exactly what serialized means, i only use it for saving data, but if you attached the text into inspector, it means it is defined. So..
If another object is doing the fade out, is beacuse some script is saying it. $$anonymous$$aybe you put another script in this second text?
Indeed, the other object had a script attached to it... It didn't have anything to do with this one, but I'll still try to remove it and see what happens, maybe start everything from scratch today when I get home from work. Thanks!
Your answer
Follow this Question
Related Questions
Script attached on prefab; cannot attach "text" component 1 Answer
How do I display AudioMixer volume as a percentage 2 Answers
UI Text created from C# Script 0 Answers
[Solved] Multiple text on a single GameObject 0 Answers
How to fix my GUI Text? 0 Answers