Hi everybody!
First question here on unityAnswers so be gentle!
I’m currently creating an air hockey game. So far, I have the drag and drop working, the collisions working to an extent but not perfectly, the scores, and I am now on creating power ups. I have them creating in random positions and even alternating ends of the table to keep it fair and only 1 on the table at a time, with 1 of 2 possible power ups which works fine. The power up gets used instantly and after 5 seconds, all goes back to normal as hoped. Where I get my problem is trying to delay the creation of power ups. Obviously, if left completely to itself, the random number generator could churn out several power ups within quick succession i.e., as soon as one is collected, another is created. Similarly, it could be ages before a new power up is created. I don’t want this, it wouldn’t make for a nicely playable game. I want it so that it has to wait for at least 10 seconds before another power up can be created and i am having serious issues.
I have a ‘RandomNumberGenerator’ script which is just attached to a GUIText object and at the moment, I have the ‘Update()’ function which calls the ‘RandomPowerup()’ function:
function Update ()
{
RandomPowerup();
}
then in my ‘RandomPowerup()’ function (edited):
function RandomPowerup()
{
yield WaitForSeconds(5); //This is the only Wait that actually works and only the first time it runs through
var random : int = Random.Range(0, 3);
var xPos : int;
var zPos : int;
if (powerOn == false) //If there isn't already a power up on the table
{
if (random == 1) //Decide which power up it is
{
if (powerPlace == true) //Decide where on the table to put it
{
xPos = Random.Range(5, 145);
zPos = Random.Range(-75, 75);
powerPlace = false;
}
else
{
xPos = Random.Range(-145, -5);
zPos = Random.Range(-75, 75);
powerPlace = true;
}
powerOn = true;
cube = Instantiate(powerUp, Vector3(xPos, 5, zPos), Quaternion.identity); //Create powerup
HandleBehaviour.powerUpType = "PaddleSizeUp"; //For debugging
yield WaitForSeconds(3); //wait after power up is created before doing anything else *****
}
if (random == 2) //All same as above
{
if (powerPlace == true)
{
xPos = Random.Range(130, 145);
zPos = Random.Range(-75, 75);
powerPlace = false;
}
else
{
xPos = Random.Range(-145, -130);
zPos = Random.Range(-75, 75);
powerPlace = true;
}
powerOn = true;
cube = Instantiate(powerUp, Vector3(xPos, 5, zPos), Quaternion.identity);
HandleBehaviour.powerUpType = "PaddleSizeDown";
yield WaitForSeconds(3);
}
}
}
*****This is the line that doesn’t do what I want it to do. It should wait for 3 seconds, but just seems to not do anything. I have seen another couple of topics (http://unity3d.qatohost.com/questions/47872/can-i-not-use-yield-waitforseconds-in-a-class-func.html) and (http://unity3d.qatohost.com/questions/23424/coroutines-waitforseconds-with-function-update.html#comment-79094) that suggest you can’t use coroutines like I’m trying to, but I’ve tried their alternatives and none of them seem to work.
Can anybody offer any support?
Thanks in advance,
Matt