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.
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);
}
}
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.
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.
At the beginning of each level just set score to 0 in the Start() function.
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);
}
}
I put together a little Test Project for you. There are three scripts
TestScript
Score$$anonymous$$anagerScript
$$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.
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,
Like how you got it here ?
void Start () { score = PlayerPrefs.GetInt("Score", 0); }
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.
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.
Your answer
Follow this Question
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