- Home /
Error Coroutine couldn't be started!
My script has been working fine for the past few weeks but suddenly I get a console error when I run it and the message I want to display on the screen isn't displayed.
Here is my script:
using UnityEngine;
using System.Collections;
public class CutSceneLight1 : MonoBehaviour {
public int wait;
public GUIStyle text;
string message = " ";
//GUI Box
void OnGUI() {
GUI.Label((new Rect(600,400,200,200)),message ,text);
}
//Displaying text
IEnumerator OnTriggerEnter(Collider otherObjective){
if (otherObjective.tag == "Player") {
message = "Message here";
StartCoroutine(message);
yield return new WaitForSeconds(wait);
message = "Another message here";
What I want to happen is for the player to cross the collider which is set as trigger and then the messages play.
Please help me I need to fix this as soon as possible. Thanks!
Answer by Ibzy · Jun 23, 2015 at 03:17 PM
Not entirely sure what you're trying to accomplish with the messages, but you are trying to call a string as a function? ("message" is a string).
Also, OnTriggerEnter should be a void. I imagine you want to apply StartCoroutine(displayMessage(message));
to an IEnumerator displayMessage(string msg);
function?
Sorry I'm a bit of a noob.
I want the messages to be displayed on the screen when you play the game and I want them to each be displayed for a certain length of time before moving on to the next message. (I've put "$$anonymous$$essage here" because I don't know what I want to be printed on the screen just yet)
I didn't use void for OnTriggerEnter because it then wouldn't let me use WaitForSeconds and so I used IEnumberator ins$$anonymous$$d and this worked perfectly until a week later when all of a sudden nothing was working, which I found very odd.
Thank you for your help so far I greatly appreciate it! Although I'm not entirely sure what you are suggesting I do, sorry, could you explain a little more?
So everything worked perfectly before? Your messages were appearing on the screen also?
Looking again this might literally be as simple as removing the line StartCoroutine(message);
Give that a go and let me know.
Answer by OctoMan · Jun 23, 2015 at 03:30 PM
Why not start the coroutine with the Trigger/collider?
void OnTriggerEnter(Collider otherObjective)
{
if(otherObjective.tag == "Player")
{
StartCoroutine(Messanger());
}
}
private IEnumerator Messanger()
{
//do your messages stuff here...
message = "Message here";
yield return new WaitForSeconds(wait);
message = "Another message here";
}
Might have some typos!
Here is a small textoutputmanager i wrote for my current project. it will show for 3 seconds a specific message and will disable after that.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextOutPutManager : MonoBehaviour {
//panel ggf.
public GameObject textPanel;
//text field output
public Text MessageOutput;
//string message
public string message = "";//this is the message to show
//string incoming message
public string incomingMessage = "this is the message send from outside";
//bool showtext
public bool showtext = false;
// Use this for initialization
void Start ()
{
textPanel.SetActive(false);
}
// Update is called once per frame
void Update ()
{
if(showtext)
{
StartCoroutine("Timer");
}
else
{
StopCoroutine("Timer");
}
}
private IEnumerator Timer()
{
message = incomingMessage;
MessageOutput.text = message;
textPanel.SetActive(true);
yield return new WaitForSeconds (3);
message = "";
MessageOutput.text = message;
textPanel.SetActive(false);
showtext = false;
}
}
This doesn't seem to work. I tried it out and I don't get any errors but the message doesn't appear on the screen. Thank you anyway.
I've just seen the rest of your comment and will try that out now. :)
I've just tried to run your script to see if it would help me, however I get an error saying that the type or namespace name 'Text' could not be found.
The namespace
using UnityEngine.UI;
needs to be in top as it is in my script, of course you need toi create a textpanel with a field inside, and drag the parts in the inspector to make it work.
To make it active then you can enable it from outside like:
private TextOutPut$$anonymous$$anager TO$$anonymous$$;
void Awake ()
{
//find the object where it is attached to
TO$$anonymous$$ = GameObject.Find ("Game$$anonymous$$anager").GetComponent<TextOutPut$$anonymous$$anager> ();
}
void send()
{
//any condition then
TO$$anonymous$$.inco$$anonymous$$g$$anonymous$$essage = "Blabla, show that message";
TO$$anonymous$$.showtext = true;
}
Answer by Tekksin · Jul 26, 2018 at 07:33 AM
Generally, this error comes up when you start a coroutine that doesn't exist. Double check the name from that message variable.