Can you help with a random event in my FPS?
In one of my FPS game's scenes, I'm placing a weapons locker that will open at random times throughout the game. Each time it opens, a randomly selected gun will appear for the player to take. I'm having issues with trying to get a basic script to work, where I want the locker door to open a random number of seconds after beginning the game, and offer one of two guns to pickup.
At the moment, the code selects a number between 3 and 7, which is the number of seconds I want it to wait before opening. However, once doing this, the code doesn't seem to move to the next stage, which is selecting one of the guns to spawn. (The gun spawn is done by the models all being in the locker and their Mesh Renderers and Box Colliders activated and deactivated accordingly.)
In the Inspector, this code is attached to the locker door, which also has the animation attached to it.
I'm not massively experienced with using timers at this point, so if anyone could help, I'd be grateful.
using UnityEngine;
public class LockerDoorOpen : MonoBehaviour
{
public AudioSource openingSound;
public int gunSpawnTimer;
public GameObject lockerDoor;
public GameObject railgun;
public GameObject suppressor;
public int spawnedGun;
private void Start()
{
gunSpawnTimer = Random.Range(3, 7);
Invoke("LockerOpenTime", 0);
}
void LockerOpenTime()
{
if (gunSpawnTimer == Time.unscaledTime)
{
Invoke("OpenLockerDoor", 0);
}
}
void OpenLockerDoor()
{
spawnedGun = (Random.Range(0, 1));
if (spawnedGun == 0)
{
railgun.GetComponent<MeshRenderer>().enabled = true;
railgun.GetComponent<BoxCollider>().enabled = true;
}
if (spawnedGun == 1)
{
suppressor.GetComponent<MeshRenderer>().enabled = true;
suppressor.GetComponent<BoxCollider>().enabled = true;
}
lockerDoor.GetComponent<Animation>().Play("LockerDoorOpen");
openingSound.Play();
}
}