- Home /
GUIText transition trouble
So I have a small, text-based cutscene that I have in my game, where the text fades when the player presses a button and new text instantly appears after three seconds. The fade and appearing aspects of my text are fine. It's just that I seem to have some trouble at the beginning of the cutscene: using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class Cutscene1Script : MonoBehaviour {
public int cutsceneProgress;
public bool startTimer;
public bool noSpawn = true;
public float cutsceneTransitionTimer = 3;
public GUIText text;
void Start (){
noSpawn = false;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1") && startTimer == false){
startTimer = true;
}
if (startTimer == true) {
cutsceneTransitionTimer -= Time.deltaTime;
}
if (cutsceneTransitionTimer <= 0) {
cutsceneProgress += 1;
cutsceneTransitionTimer = 3;
startTimer = false;
noSpawn = false;
}
if (cutsceneProgress == 1 && noSpawn == false) {
Instantiate (text, new Vector3 (0.5f, 0.5f, 0), Quaternion.identity);
text.text = "stuff";
noSpawn = true;
return;
}
if (cutsceneProgress == 2 && noSpawn == false) {
Instantiate (text, new Vector3 (0.5f, 0.5f, 0), Quaternion.identity);
noSpawn = true;
text.text = "stuff2";
return;
}
if (cutsceneProgress == 3 && noSpawn == false) {
Instantiate (text, new Vector3 (0.5f, 0.5f, 0), Quaternion.identity);
noSpawn = true;
text.text = "stuff3";
return;
}
if (cutsceneProgress == 4 && noSpawn == false) {
Instantiate (text, new Vector3 (0.5f, 0.5f, 0), Quaternion.identity);
noSpawn = true;
text.text = "stuff4";
return;
}
if (cutsceneProgress == 5 && noSpawn == false) {
Instantiate (text, new Vector3 (0.5f, 0.5f, 0), Quaternion.identity);
noSpawn = true;
text.text = "stuff5";
return;
}
if (cutsceneProgress == 6 && noSpawn == false) {
Instantiate (text, new Vector3 (0.5f, 0.5f, 0), Quaternion.identity);
noSpawn = true;
text.text = "stuff6";
return;
}
if (cutsceneProgress == 7 && noSpawn == false) {
SceneManager.LoadScene ("NextScene");
return;
}
}
}
When the player first presses Fire1, the "stuff" text shows, which is what I want. The problem is that, on the next press, the same text shows up and then after a third press, it decides to change to "stuff2."
I think I have an idea on what is going on. Basically, after the GUIText fades, it gets destroyed and a clone is instantiated. I have a feeling the initial introduction of the clone is causing the problem, but I have no clue as to how to fix the problem.
One final note: this script is on an empty gameObject, and the GUIText is defined as the initial object (it doesn't have (clone) on it via inspector).
Answer by FortisVenaliter · May 20, 2016 at 05:37 PM
It's probably because cutsceneProgress starts at zero, but your first check is for if it equals one. Try decreasing your check numbers by one.
I did that and nothing is changing. I really think it's because of the introduction of a clone.
Ohhhh! I see the problem. When you set the text string, you're setting it on the prefab, not on the instantiated object. So, it will only work after the previous one was run. You need to get the instance back from the Instantiate call and apply your string to that, not to the 'text' prefab variable.
I see what you mean!...... but I don't know what to do to fix that :/
I'm still new to this whole unity thing. I'll see if I can't figure it out, but extra input would be appreciated :D
Your answer
Follow this Question
Related Questions
Variable being shared between clones 2 Answers
cachedTextGenerator not working with instantiated canvas 1 Answer
How can I destroy an specific clone already instantiated, when there's more than one? 1 Answer
I can't get Text Mesh to disappear in the scene / How do I make the Text Mesh disappear in my scene 0 Answers
TextEditor old script issue 0 Answers