- Home /
Custom Function not working ?
Can somebody tell me why the custom function (think that's the right term) the function newFunction part of my script doesn't work. I get no errors and the rest of the script works fine but it's like the newFunction is just completely ignored by the game ?
#pragma strict
public var Boulder : GameObject;
public var BoulderLimit : int; // the max amount of Boulders that can be spawned
public var BoulderCount : int; // the current amount of spawned Boulders
public var spawnBoulder : boolean; // if the Boulders are allowed to spawn
function Update ()
{
if ( BoulderCount < BoulderLimit ) // check if the current spawn amount is below the limit
{
SpawnBoulder (); // spawn the Boulder
}
else
{
spawnBoulder = false;
Debug.Log ( "Spawn Limit Has Been Reached!" );
}
}
function SpawnBoulder () // spawn the GameObject
{
Instantiate(Boulder, transform.position, transform.rotation);
BoulderCount++; // add to the current amount of spawned Boulders
Debug.Log ( "Spawned GameObject #" + BoulderCount );
newFunction();
}
function newFunction()
{
yield WaitForSeconds(8);
BoulderCount--;
Debug.Log ( "Boulder Count Reset!" );
}
Any ideas guys ?
Answer by meat5000 · Dec 21, 2014 at 04:32 PM
Your functions needs to be upgraded to a coroutine.
yields and waitforseconds can only be used there.
unfortunately that's beyond my level of coding at the moment. Will look into it though, thanks
Without wanting to make myself seem dumb, I thought that's what I'd done? I called a new function, put my WaitForSeconds with a yield statement, but the rest of the function doesn't happen? I only want the bouldercount to reset to zero
Yeah you're right, I'm being dumb and looking at the C# page.
Your function should work just fine.
Do you see Spawned GameObject #" + BoulderCount Debug?
and
Boulder Count Reset! ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613174554im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Yield Never Works 4 Answers
Setting Scroll View Width GUILayout 1 Answer
Trying WaitForSeconds before Instantiating 2 Answers
Scripting Heart BPM Using Different Values With Switch & Yield 2 Answers
yield waitforseconds doesnt work on function start 2 Answers