I have a program that randomly chooses a number between 1 and 3, and therefore chooses an enemy to fire a laser. But sometimes when an enemy is picked, nobody else shoots but him. randomNumber doesn’t change. Please Help! I have no idea and I’ve tired over this for days!
P.S nextShot and period is my way of creating a pause in the update function. It might be the problem but once again, I don’t know!
P.P.S in another script are the commands on where the laser moves. @aldonaletto
I don’t use JavaScript so I apologise for any potential syntax errors on my part but try this:
function Update ()
{
if(Time.time > nextShot) // Stopped checking for this multiple times as it was unecessary.
// Not entirely sure what it's doing but you said you need it for
// pause functionality so I left it alone.
{
nextShot = Time.time + period; // Again not entirely sure what this is doing
// but you said you needed it.
if(evilEntity1Fire == true) // Changed to an else if block as it seems two robots
// can't shoot at the same time.
{
var laserPrefab1 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity1.transform.position, evilEntity1.transform.rotation);
evilEntity1Fire = false;
}
else if(evilEntity2Fire == true)
{
nextShot = Time.time + period;
var laserPrefab2 : GameObject;
laserPrefab2 = Instantiate(laser, evilEntity2.transform.position, evilEntity2.transform.rotation);
evilEntity2Fire = false;
}
else if(evilEntity3Fire == true)
{
var laserPrefab3 : GameObject;
laserPrefab1 = Instantiate(laser, evilEntity3.transform.position, evilEntity3.transform.rotation);
evilEntity3Fire = false;
}
}
Invoke("Robot", difficultyWait);
}
function Robot ()
{
randomNumber = Random.Range(1,4); // The documentation states "The returned value will
// never be max" so 3 wouldn't have ever occurred.
// I also suspect this does not need to be a class
// variable just a local.
if(randomNumber == 1)
{
evilEntity1Fire = true;
}
else if(randomNumber == 2)
{
evilEntity2Fire = true;
}
else if(randomNumber == 3)
{
evilEntity3Fire = true;
}
}
I have tried to tidy up your code as well. I hoped this helped.
EDIT :
I fixed it!
Thanks Tohaveaname, your code was awesome. The problem that we both ovelooked was evilEntityfFire(x) not turning off. I just moved all the statements saying evilEntityFire(x) = False outside the if statements and just inside the Update() Function. Thanks so much for your help though Tohaveaname, that was the only thing I changed.
Wooh!