Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by BluePinguin · May 31, 2015 at 08:26 AM · c#guidestroyvariable

[Fixed] Unity UI Text Code Reference magically dissapears

Hey People!

We're busy trying to show product information in an UI. For testing purposes we started with trying to just show the title.

So we have a UIController.cs that handles UI. You can find the source code below for reference.


What happens:

  • A new UI is loaded in the Scene at runtime

  • A UIController script is placed in the scene

  • The UIController uses GameObject.FindObjectWithTag to find the correct Text element

  • The UIController's method PopulateUI(Product p) is called

  • Textfield should be filled (Which doesn't happen).


The Analysis we've done:

  • The UI Text Component reference is private: Can't be set outside of class

  • Is the Text Component actually read from the scene? Using Debug.Log("[Start] TitleLabel is null: " + titleLabel == null); -- Yes

  • Is the Text Component still referenced right before setting the text? Using Debug.Log("[PopulateUI] TitleLabel is null: " + titleLabel == null); -- No (This is wierd)

  • So we check if the moment that the Text component is read from the scene is actually triggered before the PopulateUI call using Debug.Log -- Yes

  • Is the Text component correctly linked? Using [SerializeComponent] -- Seems to be, but when the reference is set to null in code the link in the editor stays, so this is unreliable output.

  • Maybe there are multiple UIControllers in the scene? Check using Debug.Log in the Awake method. -- No, prints only once.


Then how come the Reference to the Text component is gone? There is no way externally to set the variable since it's private and the only time it gets set is at the Start method. I've included the entire script with output below.

I'm hoping that someone is fimiliar with this problem and can help me out.


Output:

Output


Source:

 public class UIController : MonoBehaviour {
     [SerializeField]
     private Text titleLabel;
 
     private string titleTag = "ProductTitleField";
 
     /// <summary>
     /// Populates the Environment UI with the product info
     /// </summary>
     /// <param name="p">The Product which contains the data of everything that needs to be populated</param>
     public void PopulateUI(Product p) {
         Debug.Log("[PopulateUI] TitleLabel is null: " + titleLabel == null);
         FillLabelIfPossible(titleLabel, p.Title);
     }
 
     private void Awake() {
         Debug.Log("An Instance of UIController Exists");
     }
 
     private void Start() {
         titleLabel = GetLabelByTag(titleTag);
 
         Debug.Log("[Start] TitleLabel is null: " + titleLabel == null);
     }
 
     private Text GetLabelByTag(string tag) {
         GameObject go = GameObject.FindGameObjectWithTag(tag);
 
         if (go == null) {
             Debug.LogWarning("Could not find a GameObject tagged with " + tag + ".");
             return null;
         }
 
         Text t = go.GetComponent<Text>();
 
         if (t == null) {
             Debug.LogWarning("Could not find a Text Component on the GameObject tagged with " + tag + ".");
             return null;
         }
 
         return t;
     }
 
     private void FillLabelIfPossible(Text label, string text) {
         if (label != null) {
             label.text = text;
         }
     }
 }


Comment
Add comment · Show 4
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 fafase · May 31, 2015 at 08:46 AM 1
Share

As a first debug, you could set your inspector to Debug mode so that you can see your label reference in the editor.

Second, you could add a simple component to your found object to see if it gets destroys:

 public class Test:$$anonymous$$onoBehaviour{
     void OnDestroy(){
         Debug.Log("Destroy object");
     }
 }


then:

  GameObject go = GameObject.FindGameObjectWithTag(tag);
  go.AddComponent<Test>();
avatar image BluePinguin · May 31, 2015 at 01:36 PM 0
Share

Thanks fafase for the idea! I've added the script but nothing pops up. When I delete the gameobject at runtime manually the log does appear. I attachedthe script on the GO of the Text element and the GO of the UIController.

I've made the reference a [SerializableField] so Debug mode shouldn't make a difference, I did it anyway but nothing changed. I still could see the Text element reference as instantiated while the debug said the reference was null.

avatar image fafase · Jun 07, 2015 at 01:52 PM 0
Share

It says "Fixed", you should add an edit to let others know what was wrong.

avatar image BluePinguin · Jun 09, 2015 at 05:28 PM 0
Share

I've done that! As an Answer

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by BluePinguin · Jun 08, 2015 at 07:36 AM

I've fixed the issue. The issue occured because of wrongful Event subscription on my part. When a new environment was loaded the ShopManager subscribed on the UIController to be destroyed instead of the newly instantiated. The scene was destroyed but the UIController class was not gathered by the garbage collector because it was still referenced through script.

Oh well, it's fixed now.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

C# How To Save PlayerPrefs for Unity GUI 2 Answers

UI button change variables in another script 1 Answer

How to store a prefab in a variable 1 Answer

Distribute terrain in zones 3 Answers

destroty gui object 1 Answer


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