I am a beginner so please make your explanations simple. I am also coding in C#.
I would like to get a score variable to output into a pre-existing UI Text. I found some useful code at Inputting a variable into UI Text - Questions & Answers - Unity Discussions but I don’t know how do incorporate into my current code? I tried but had many compiler errors and ended up removing it to consult the forum.
The code I currently have is…
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CheckAnsL1 : MonoBehaviour
{
public InputField iField;
string myName;
string myText;
public int score = 0;
void Start()
{
if (PlayerPrefs.HasKey("Score") == true)
{
score = PlayerPrefs.GetInt("Score");
}
}
public void MyFunction()
{
Debug.Log(iField.text);
myName = iField.text;
if (myName == "city")
{
score++;
Debug.Log("Correct! The word 'city' is correct!");
Debug.Log("Your score is now:");
Debug.Log(score);
PlayerPrefs.SetInt("Score", score);
}
else
{
score--;
Debug.Log("Incorrect! The answer was 'city'.");
Debug.Log("Your score is now:");
Debug.Log(score);
PlayerPrefs.SetInt("Score", score);
}
}
}
It works perfectly.
Now I want to put this code into it. REMEMBER" Please tell me exactly what line to place it on or if it neds to go into a separate script of its own.
GameObject myTextgameObject; // gameObject in Hierarchy
Text ourComponent; // Our refference to text component
void Start () {
// Find gameObject with name "MyText"
myTextgameObject = GameObject.Find("MyText");
// Get component Text from that gameObject
ourComponent = myTextgameObject.GetComponent<Text>();
// Assign new string to "Text" field in that component
ourComponent.text = "Hey, I'm some randoms score !!";
}
I want the score variable in the current script to constantly update and show in a UI text from the beginning of the scene, and for this to work across multiple scenes.
Please help! - You’re help would be much appreciated!
Since this is one of the most basic things to do in c# and unity, maybe you should try your own again and post the compiler error. That way somebody can tell you what you did wrong and you can learn a bit.
If you want to build your own car, you have to know why scratching a screw with a spoon won’t work.
But I don’t even know where to paste the code in the first place? Please just tell me. If there are any further compiler errors I will upload a screenshot. @Carpe-Denius .
As soon as you understand what each line of code does, you know where to paste it.
Basically, change every Debug.Log(score); with ourComponent.text=score.ToString();
You will get an error that ourComponent is not available, so you have to search for a place where you can init the text component. I’ll leave that to you, some homework
But @Carpe-Denius I can’t just change every Debug.Log(score); with ourComponent.text=score.ToString(); because I can’t even get the rest of the code which is
Code (CSharp):
GameObject myTextgameObject; // gameObject in Hierarchy
Text ourComponent; // Our refference to text component
// Assign new string to “Text” field in that component
ourComponent.text = “Hey, I’m some randoms score !!”;
}
Because the script it is in now already has a void Start() so do i just add all of the new code above under the existing code’s Start()? I AM SO CONFUSED??? Please help
You need to take at least some basic tutorials in programming, C# to be specific. We cant do everything for you, we have our own stuff we are working on.
We provided you with the information you need to do what you want, as to exactly how to do it, you will need to learn yourself.
I would like to know what code is necessary to make a pre-existing UI button visible at the end of a pre-existing C# Script. PS: Does this mean the button has to be set to invisible first? How do I do any of this. Is any of this even possible?
@Lee7 what is IIRC? I assume that is the line of code I need to make a GUI object invisible you can just disable it via script and enable it when you want.
@Lee7 & @CarpeDenius thanks for your help! I fixed all the errors myself! So proud! I thank you for your help so far and will contact you if needed in the future! Thanks again!
I have the code below which works perfectly. I would like to somehow (in a new scene) pull the score variable from the previous code and display it in a UI text. I would also like the new script in the new scene to run automatically without any triggers?
Any help would be much appreciated!
Here is my code…
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CheckAnsL8 : MonoBehaviour
{
public InputField iField;
string myName;
string myText;
public int score = 0;
public Text Status;
GameObject myTextgameObject; // gameObject in Hierarchy
Text ourComponent; // Our refference to text component
void Start()
{
if (PlayerPrefs.HasKey("Score") == true)
{
score = PlayerPrefs.GetInt("Score");
}
// Find gameObject with name "MyText"
myTextgameObject = GameObject.Find("MyText");
// Get component Text from that gameObject
ourComponent = myTextgameObject.GetComponent<Text>();
}
public void MyFunction()
{
Debug.Log(iField.text);
myName = iField.text;
if (myName == "castle")
{
score++;
Debug.Log("Correct! The word 'castle' is correct!");
Debug.Log("Your score is now:");
Debug.Log(score);
PlayerPrefs.SetInt("Score", score);
Status.text = "Correct!";
Status.color = Color.green;
}
else
{
score--;
Debug.Log("Incorrect! The answer was 'castle'.");
Debug.Log("Your score is now:");
Debug.Log(score);
PlayerPrefs.SetInt("Score", score);
Status.text = "Incorrect!";
Status.color = Color.red;
}
GameObject theTargetChild = GameObject.Find("theTargetChild");
foreach (Transform child in theTargetChild.transform)
{
child.gameObject.SetActive(true);
}
}
void Update () {
ourComponent.text = score.ToString();
}
}