- Home /
Instantiating, methods and returning from a seperate script.
Hi there, so what I'm trying to do is create a warning window that pops up when the user selects something wrong. However this warning window changes its text often so I don't want to make multiple ones. It needs to be 100% dynamic.
The problem I'm having is that I have a script that instantiates the warning window prefab when the user does something wrong. This warning window has a cancel button and an accept button (Do they want to cancel what they are doing or accept). If cancel is clicked it just destroys the warning window gameObject. But when accept is clicked I want it to execute a method in the script that created the warning window.
I've thought of different ideas that could happen but I don't know if any are possible or can be done.
When the warning window prefab is created in the method, pause execution of that method and wait for a return from the script. Perhaps using IEnumerable to pause execution somehow?
In the warning window prefab script, get a reference to the script that created it, then call a method from that script.
These are just a couple ideas I had, does anyone know of a better way or how this could work?
Answer by b1gry4n · May 30, 2018 at 11:09 PM
have the warning window always exist, dont instantiate it, and have references to its "text" component. when you want to display a warning, enable the gameobject after you modify its text, when you want to disable it, just disable the gameobject. if theres only going to be one warning window you can make it static, that way no script needs a reference to it that you have to manually set up. you could try something like this...
public static WarningWindowManager _this;
public Text warningText;
public GameObject warningWindow;
bool isDisplaying = false;
private void Awake()
{
//you can only have one instance of "WarningWindowManager" at any time..so if one already exists we need to destroy this one
if (WarningWindowManager.Instance() != null)
{
Destroy(this.gameObject);
}
else
{
_this = this;
}
}
public static WarningWindowManager Instance()
{
return _this;
}
public static void DisplayWarning(string warningMessage, float forTime)
{
_this.WarningWindowSetup(warningMessage, forTime);
}
public void WarningWindowSetup(string message, float time)
{
//if we are already displaying, we need to interupt the coroutine as its already waiting to disable the game object. this will refresh the timer
if (isDisplaying)
{
StopAllCoroutines();
}
//set the message to the Text component
warningText.text = message;
//start the coroutine, display the message for X seconds
StartCoroutine(WaitForTime(time));
}
IEnumerator WaitForTime(float time)
{
isDisplaying = true;
warningWindow.SetActive(true);
yield return new WaitForSeconds(time);
isDisplaying = false;
warningWindow.SetActive(false);
warningText.text = "";
}
and any time you want to display a warning...
WarningWindowManager.DisplayWarning("warning!", 5.0f);
dont forget to add using UnityEngine.UI;
since you will be using the Text component
Your answer
Follow this Question
Related Questions
Object Spawner 1 Answer
Distribute terrain in zones 3 Answers
Instantiate prefab (button, texture) with attached script 1 Answer
what is wrong whis this? 1 Answer
Multiple Cars not working 1 Answer