I am currently keeping score in my app via a button with this code below. I would like to make it create text instead but not sure what to do to change it. Also I am not a professional so dumb it down please and thank you.
using UnityEngine;
using UnityEngine.SceneManagement;
// Object.DontDestroyOnLoad example.
//
// Two scenes call each other. This happens when OnGUI button is clicked.
// scene1 will load scene2; scene2 will load scene1. Both scenes have
// the MenuGameObject with the SceneSwap.cs script attached.
//
// AudioSource plays an AudioClip as the game runs. This is on the
// BackgroundMusic GameObject which has a music tag. The audio
// starts in AudioSource.playOnAwake. The DontDestroy.cs script
// is attached to BackgroundMusic.
public class SceneSwap : MonoBehaviour
{
private void OnGUI()
{
int xCenter = (Screen.width / 2);
int yCenter = (Screen.height / 2);
int width = 400;
int height = 120;
GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle(“button”));
fontSize.fontSize = 32;
if (scene.name == “scene1”)
{
// Show a button to allow scene2 to be switched to.
if (GUI.Button(new Rect(xCenter - width / 2, yCenter - height / 2, width, height), “Load second scene”, fontSize))
{ SceneManager.LoadScene(“scene2”);
}
}
else
{
// Show a button to allow scene1 to be returned to.
if (GUI.Button(new Rect(xCenter - width / 2, yCenter - height / 2, width, height), “Return to first scene”, fontSize))
{ SceneManager.LoadScene(“scene1”);
}
}
}
}
The OnGUI-based UI system is at this point wildly obsolete (and bad), and whatever tutorial or sample code you found it on is outdated by 5+ years. The modern UI system is called “Unity UI” - search for tutorials on that. (Edit: Upon searching for some of the commented text I see that this is an example from the Unity documentation, so I can hardly blame you for using it, but it is obsolete and really shouldn’t be there. I’m going to report it as an issue in the documentation.)
Could you clarify what you mean by “create text instead”?
So I have an app that has about 20 scenes and based on what button you pick you go to a correct incorrect scene. I would like to create text based on what buttons I have clicked for a score. Someone told me I should jut clear the data but quite honestly I don’t know how to do that and just created about 60 scenes. So if you can help lead me somewhere for either that’s great.
Also I just created this code and would use something like this anyway to add on based on button clicks?
using UnityEngine;
using System.Collections;
public class Text : MonoBehaviour
{
public Rect windowRect = new Rect(20, 20, 120, 50);
void OnGUI()
{
// Register the window. Notice the 3rd parameter
windowRect = GUI.Window(0, windowRect, DoMyWindow, "Score");
}
// Make the contents of the window
void DoMyWindow(int windowID)
{
if (GUI.Button(new Rect(10, 20, 100, 20), "1/20"))
{
print("Got a click");
}
}
}
When you say you have a bunch of scenes, are you saying you have a bunch of scenes where the only difference is what text is being shown? If so, then you should just be setting the .text value of a Text component (as in the Unity Text class, not your script - naming it “Text” is going to be problematic later as it will conflict with the builtin class name and cause confusion), and this functionality will be triggered with an event set up in a Button object in the UI.
The above things are basically “UI 101” and would be covered by any basic-level Unity UI tutorial (including this one), so I’ll reiterate that you should check out a tutorial on the modern UI system. If you have questions or problems with the tutorial as you go through it, you can come back here and ask.