Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by amorimph · Aug 01, 2018 at 10:45 PM · scripting problemtextcanvasserializefield

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amorimph · Aug 02, 2018 at 10:54 AM 0
Share

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?

avatar image tormentoarmagedoom amorimph · Aug 02, 2018 at 11:03 AM 1
Share

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?

avatar image amorimph tormentoarmagedoom · Aug 02, 2018 at 01:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

226 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges