I want the “print” message to display once. The problem is that it’s in the Update function so it prints to infinity as long as it’s true.
using UnityEngine;
using System.Collections;
public class Quest1Door : MonoBehaviour {
void Update()
{
if (Quest1.openedDoor) {
print("The door is now open.");
}
}
}
I tried putting break/return after it, but it’s not a loop?
I also did a doOnce variable that was set to true, but it seems like a lot of extra code for something so simple. Then again, this is how Fallout 3/Oblivion scripts are done.
Don’t use Update, which always runs every frame regardless, and is very poor for any kind of event scheduling. Just call the function when you need to.
public class Quest : MonoBehaviour {
void PrintMessage (string message)
{
print message;
}
}
Get a reference to the Quest script, and when the player opens the door, do
questScript.PrintMessage ("The door is now open.");
You need to use a boolean flag that you turn on and off when needed:
using UnityEngine;
using System.Collections;
public class Quest1Door : MonoBehaviour {
private bool printDone;
void Start()
{
printDone = false;
}
void Update()
{
if (Quest1.openedDoor && !printDone) {
print("The door is now open.");
printDone = true;
}
}
}
By setting “printDone” to true in the IF statement, your print() text won’t be displayed again until “printDone” is set back to false (for example, the next time you restart the scene).
Hope this helps, I couldn’t test the code but it should work.
Stephan