Function Update not working
I have a javascript that is supposed to let you pickup a battery but other stuff happens but when the script gets to the function update, i get errors. Can someone PLEASE help
var monster : GameObject;
var SoundObject : GameObject;
var MeshObject : GameObject;
var playerTag = "Player";
var buttonInRange;
private var hasPlayed = false;
function OnTriggerEnter (c : Collider)
{
buttonInRange = true;
}
function OnTriggerExit (c : Collider)
{
buttonInRange = false;
}
function OnGUI ()
{
if(buttonInRange == true)
{
GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 120, 50), "Pick up batteries");
}
function Update ()
{
if (buttonInRange == true)
{
if (Input.GetKeyDown ("e"))
{
if(!hasPlayed)
{
MeshObject.GetComponent.<MeshRenderer>().enabled = false;
yield WaitForSeconds(0.5);
SoundObject.active = true;
yield WaitForSeconds(2.5);
monster.active = true;
SoundObject.active = false;
hasPlayed = true;
Destroy(gameObject);
}
}
}
}
Looks to me like a parsing error. $$anonymous$$issing some "{ }" in your code, so the functions aren't closed. Line 27
I just did that, but i get the error "Update() can not be a coroutine." Any Ideas? @Lord_Ford
Hey i found the problem! The yield WaitForSeconds is causing it. But this script absolutely needs the yield WaitForSeconds. Is there a way to prevent it from causing an error?? @Lord_Ford
Answer by mateo4632 · Feb 07, 2016 at 03:35 AM
You can't have yield WaitForSeconds in a function, it needs to be in a coroutine.
IEnumerator Example()
{
if (buttonInRange == true)
{
if (Input.GetKeyDown ("e"))
{
if(!hasPlayed)
{
MeshObject.GetComponent.<MeshRenderer>().enabled = false;
yield WaitForSeconds(0.5);
SoundObject.active = true;
yield WaitForSeconds(2.5);
monster.active = true;
SoundObject.active = false;
hasPlayed = true;
Destroy(gameObject);
}
}
}
}
But since you need this checked every frame add this before it.
Function Update()
{
StartCoroutine("Example");
}
This should work please let me know if this helped you!
Your answer
Follow this Question
Related Questions
Array index is out of range. 1 Answer
Error BCE0051 with script JS 1 Answer
Javascript error 1 Answer
MonoDevelop not starting even though all assemblies installed, still not answered! 2 Answers