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 importguru88 · Jun 23, 2016 at 06:00 PM · uitextcanvaschildchildren

How do I keep child gameobject of UI text

I need to obtain the children of the ui text so I can switch scenes . The script I have made out is not working . When I press play the scenes the switch . What I trying to do is carry over the text.

alt text

I just need this score script corrected :

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ScoreManagerScript : MonoBehaviour
  {
 
  
      public static int score;
 
 
 
      private Text text;
      
      void Awake()
      {
          text = GetComponent <Text> ();
          score = 0;
 
 DontDestroyOnLoad(transform.gameObject);
      }
      
 
      
      void Update()
      {
         text.text = "Score: " + score;
          Debug.Log("Score: " + score);
      }
  
      
      
  
 }


dontdestroyonload.png (188.9 kB)
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
1
Best Answer

Answer by TBruce · Jun 23, 2016 at 06:19 PM

Remove the DontDestroyOnLoad().

What you are really interested in is keeping score from one scene to another. You should use PlayerPrefs to save and load the score value in your game.

You can do this every time you modify the score variable

 PlayerPrefs.SetInt("Score", score);

Now when loading the scene lets say in Start() do this

 void Start ()
 {
     score = PlayerPrefs.GetInt("Score", 0);
 }

To learn more about PlayerPrefs see the docs here.

Comment
Add comment · Show 22 · 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 importguru88 · Jun 24, 2016 at 03:14 AM 0
Share

I want to keep the score only on one level. When the next level start the score right back at zero. I dont want the from the score to be previous level to be on the next level. Every level the player score is going start at zero.

avatar image TBruce importguru88 · Jun 24, 2016 at 03:56 AM 0
Share

At the beginning of each level just set score to 0 in the Start() function.

avatar image importguru88 TBruce · Jun 24, 2016 at 07:13 PM 0
Share

How do I set up player prefs on my countdown timer ?

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.Scene$$anonymous$$anagement; public class $$anonymous$$yClockScript : $$anonymous$$onoBehaviour {

        public int  $$anonymous$$inutes = 0;
        public int  Seconds = 0;
    
        private Text    m_text;
        public float   m_leftTime;
    
        public void Awake()
        {
            m_text = GetComponent<Text>();
            m_leftTime = GetInitialTime();
            DontDestroyOnLoad(transform.gameObject);
  
        
        }
    
        public void Update()
        {
            if (m_leftTime > 0f)
            {
                //  Update countdown clock
                m_leftTime -= Time.deltaTime;
                $$anonymous$$inutes = GetLeft$$anonymous$$inutes();
                Seconds = GetLeftSeconds();
  
                //  Show current clock
                if (m_leftTime > 0f)
                {
                    m_text.text = "Time : " + $$anonymous$$inutes + ":" + Seconds.ToString("00");
  
                }
                else
                {
                    //  The countdown clock has finished
                    m_text.text = "Time : 0:00";
                }
            }
        }
    
        private float GetInitialTime()
        {
            return $$anonymous$$inutes * 60f + Seconds;
        }
    
        private int GetLeft$$anonymous$$inutes()
        {
            return $$anonymous$$athf.FloorToInt(m_leftTime / 60f);
        }
    
        private int GetLeftSeconds()
        {
            return $$anonymous$$athf.FloorToInt(m_leftTime % 60f);
        }
    }
Show more comments
Show more comments
avatar image TBruce importguru88 · Jun 26, 2016 at 03:46 AM 0
Share

I put together a little Test Project for you. There are three scripts

  1. TestScript

  2. Score$$anonymous$$anagerScript

  3. $$anonymous$$yClockScript

The TestScript is attached to the Canvas GameObject. The Score$$anonymous$$anagerScript is attached to a Text object named Score underneath the Canvas GameObject. And $$anonymous$$yClockScript is attached to a Text object named Clock also underneath the Canvas GameObject.

There are two scenes, Level1 and Level2. Both are duplicate scenes with the exception that some of the values are different.

There is also another Text object in each scene called Scene Name. Play with the project, exa$$anonymous$$e the code.

I also modified the clock so that it does not run so fast. It counts down in seconds properly.

You can download the Test Project here.

avatar image TBruce importguru88 · Jun 28, 2016 at 06:29 PM 0
Share

Could you place this in a new question please? This question is to big and takes forever to find replies.

You can place @$$anonymous$$avina on it if you want to. Thanks,

Show more comments
avatar image importguru88 · Jun 24, 2016 at 07:10 PM 0
Share

Like how you got it here ?

void Start () { score = PlayerPrefs.GetInt("Score", 0); }

avatar image TBruce importguru88 · Jun 25, 2016 at 02:49 AM 0
Share

I am sorry if I am not following you. But first just let me reiterate you do not need to use DontDestroyOnLoad().

Having said that doing this whenever the score changes will save it to PlayerPrefs

 PlayerPrefs.SetInt("Score", score);

Now lets say you have a class that you want to use in each scene and you always want score to start from 0. Either set score to 0 in Awake() or Start().

Now you say that you just want the text to go over to the next scene. Do you mean this text "Score: " as seen here?

 text.text = "Score: " + score;

If this is the case "Score: " is a constant and does not change. If you want you and do this

 text = GetComponent <Text> ();
 text.text = "Score: 0";

In the Awake() function.

Please elaborate the text that you are trying to preserve.

avatar image importguru88 TBruce · Jun 25, 2016 at 03:18 PM 0
Share

Yes. Score : . I have that figure out . I have another ui text . $$anonymous$$aybe its the script I am using is to access the score script and timer script not working . Do you $$anonymous$$d testing this script ? I will summit the script in an hour from now. The accessing script I have is small.

Show more comments
Show more comments

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

65 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

Related Questions

Canvas Text Help 0 Answers

How can I check if there is no free space in the Text component? 1 Answer

Canvas Renderer event 0 Answers

Problems accessing child text from canvas 0 Answers

Show a text in the position of a GameObject 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