Fun challenge. I completed all three scenes (0,1,2) but I wanted to do something a little different in scene 2. In scene 1, the number is displayed. When the correct number is guessed, the exercise advances to scene 2. In scene 2, I want to again display that correct number. I’ve been reading through the C# reference manual, but so far… no solution. Does anyone know how to make my idea work?
Hi @Glenn1042!
I had the exact idea you had, but it took me ages to implement it.
Turns out (unsurprisingly) that it’s quite simple!
I copied the guess number text in unity from scene 1 to scene 2. The problem is that the NumberWizard script is run again once the new scene is up, so when I got to the end scene it would have a new random number instead of the old one.
I found out about static variables - these won’t change if they’re referenced in other scripts/scenes.
So I removed the NumberWizard script from the gameobject in the end scene, and created a new one.
In your number wizard text, you need to make the Guess Variable a static variable
I then in my new script (I called it FinalGuessScript), you just need to create a serialized TextMeshProUGUI, and then write a method for inputting the guess variable from the NumberWizard script into the TextMeshProUGUI thing. Hope this makes sense!
To access the variable from another script you need to first call the class, then the variable.
So in the game scene, mid-game, you’re using
guess.ToString();
to input the variable into text to show in the game, but in the second script you’re going to put
NumberWizard.guess.ToString();
Then you attach the new script to the gameobject components in the last scene (make sure NumberWizard script isn’t anywhere in the last scene)
And hey presto!
This is the whole code of the second script that works for me:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class FinalGuessScript : MonoBehaviour
{
[SerializeField] TextMeshProUGUI finalGuess;
// Start is called before the first frame update
void Start()
{
FinalGuess();
}
public void FinalGuess()
{
finalGuess.text = NumberWizard.guess.ToString();
}
}
Let me know if you have any questions! I’m extremely new to coding (this course is my first dive into this world) but hopefully I can help!
Thanks,
John
Thank You @John_Cruse. Your code solved the problems I was having. Now my game is completed. I did the build and run and the game runs fine.
Following the final comments in this lesson, I then put the game file (ABA.exe) onto another PC but it does not run. It says it needs the ABA_Data folder and the UnityPlayer.dll.
Hi Glenn,
I made the same mistake at first.
When it builds, it generates lots of files and folders - when you build it, make sure to make a new folder to put it in, then when you want to transfer it, transfer the whole folder. Then the .exe should work