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 /
avatar image
0
Question by RewForeN · Mar 19, 2019 at 09:50 AM · uitextsceneunityeditorscene-switching

Text.text doesnt work after scene change

This problem is just weird... I have a "Shop" scene with a Text displaying the user's current in-game currency.

The Text is assigned to "txt_money" through the unity editor and works perfectly when you start the game in the Shop scene. But when i change scene and come back to the Shop i get the following error:

MissingReferenceException: The object of type 'Text' has been destroyed but you are still trying to access it.

Further I inserted some debug.log's to see if the object exists when changing scenes and it EXISTS IN UPDATE, but DOES NOT EXIST IN the "Purchase()" method.

:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 
 public class Shop : MonoBehaviour
 {
     public int money;
     public Text txt_money;
 
     private void Update() {
         // txt_money exists here...
         Debug.Log(txt_money);               // Always prints "Text (UnityEngine.UI.Text)"
     }
 
     public void Purchase(int ammount) {
         // txt_money does not exist here ?
         Debug.Log(txt_money);               // Prints "null"
         money += ammount;
         txt_money.text = money.ToString();
     }
 }

:

Im not sure what to try to fix it, any suggestions?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by RewForeN · Mar 19, 2019 at 10:40 AM

Still dont know why the original code didnt work but found a work around:

 public class Shop : MonoBehaviour
 {
     public static Shop Instance {set; get;}
 
    private void Awake() {
         Instance = this;    
     }
 }

:

Then where "Purchase()" is called:

 Shop.Instance.Purchase(ammount);

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
avatar image
0

Answer by dargonknight · Mar 19, 2019 at 09:56 AM

@RewForeN where is the purchase function called? maybe it's called before the object initialize properly? start by removing it from everywhere and having it on a button, click that button after few seconds if it's still an not valid maybe assign in the beginning of the function and check what happens next in the editor after following those steps and dealing with the result let me know if things work out.

Comment
Add comment · Show 2 · 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 RewForeN · Mar 19, 2019 at 10:42 AM 0
Share

It is called from another script that processes purchases through google play games

avatar image dargonknight · Mar 19, 2019 at 12:35 PM 0
Share

@RewForeN that's why it didn't work the call was happening before everything else, the process i described is 1 of the easiest when you want to debug such issues, keep it in $$anonymous$$d whenever ur facing such issues

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

183 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

Related Questions

UI Scene ending with Text 1 Answer

Previous UI text sticks around even after reloading scene using SceneManager.LoadScene(scene.name); 0 Answers

Get Text from an Input Field on one scene and use it as a text on another scene Unity c# 0 Answers

How do I switch scenes by obtain a children ui canvas text 0 Answers

I want to bring the text written in Input field in the gameplay scene to the white place in the wrong scene. 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