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 importguru88 · Jun 18, 2016 at 05:43 AM · c#uitextfunctiondontdestroyonload

How do I call an DontDestroyOnLoad function on UI text

alt text

alt text

I called an don'tdestroyOnLoad function on two scripts and it doesn't seem to work . I need to carry the ui text over to the next scene. I don't have any errors . I also tried to fix this problem by adding a gameobject tag in the awake function on the test script. I don't know what to do from here. I need help .

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

using UnityEngine.SceneManagement;

public class Test : MonoBehaviour {

private MyClockScript myClock;

public GameObject Score;

private ScoreManagerScript scoremanager;

void Awake () {

myClock = GetComponent();

GameObject Score = GameObject.FindWithTag("Tip");

scoremanager = GetComponent();

 }
 

 void Update () 
 {
     if (myClock.m_leftTime < 0 && 0 < ScoreManagerScript.score)

     {

         SceneManager.LoadScene("j");
         
     }
     

}

}

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class ScoreManagerScript : MonoBehaviour {

  public static int score;
  


  private Text text;
  
  void Awake ()
  {

text = GetComponent ();

score = 0;

DontDestroyOnLoad(gameObject);

  }
  

  
  void Update()
  {

text.text = "Score: " + score;

Debug.Log("Score: " + score);

}

build-settings.png (215.6 kB)
score.png (232.3 kB)
Comment
Add comment · Show 1
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 phxvyper · Jun 18, 2016 at 07:31 PM 0
Share

Select all of your code in the editor then hit the code button that looks like "101010". Correct formatting is an important part to asking questions on here.

If its too much work for people to decipher what your question is asking or what your code does, then most people won't spend the energy to answer the question.

2 Replies

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

Answer by Sagi02 · Jun 18, 2016 at 07:27 PM

You have to call DontDestroyOnLoad on the root object, here the "canvas" object. What is happening here is that Unity destroys the canvas when loading a scene, which in turn destroys your UI, even though it was marked DontDestroyOnLoad. If you want only some UI elements to get carried over different scenes, you will have to delete the other ones manually.

Comment
Add comment · Show 1 · 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 C8H11N02 · Mar 26, 2020 at 09:01 AM 0
Share

How i can delete ?

avatar image
0

Answer by importguru88 · Jun 18, 2016 at 11:54 PM

Where at do I call a DontDestroyOnLoad on root canvas ? Delete what I dont understand.

Comment
Add comment · Show 1 · 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 Sagi02 · Jun 19, 2016 at 08:02 AM 0
Share

you have to set a reference to the "canvas" object in your script and pass it to DontDestroyOnLoad:

 using UnityEngine;
 using System.Collections;
 
 public class yourClass : $$anonymous$$onoBehaviour
 {
 public GameObject rootCanvas;
 
 void Awake()
 }
 DontDestroyOnLoad(rootCanvas);
 }
 }

ps: this code isn't proven to be working, it's just a sample, but you get the idea.

Then, in the editor, drag the canvas object into the "rootCanvas" box.

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

6 People are following this question.

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

Related Questions

Maintaining Text Size Ratio With Screen 0 Answers

if statement not working for user input 0 Answers

Why will my button not let me run a function? 4 Answers

Quick Question on UI Text Alignment 1 Answer

Positions of UI Text characters. 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