I am presently working on translating a VR dodgeball game to the Oculus Quest 2, which sees the player use a ball to deflect oncoming balls. In this game, as the “Start Game” button is pressed, a coroutine should start, spawning the dodgeballs in the throwers’ hands. In the Vive/SteamVR version of the application, the menu cannot be accessed by the player in the headset, instead an administrator manages the menu from the computer. The coroutine is shown below:
public void spawnBall() {
ball = Instantiate(ballPrefab, ballSpawnLocation.position, Quaternion.identity);
ball.transform.localScale = new Vector3(EnemyController.ballSize, EnemyController.ballSize, EnemyController.ballSize);
ball.transform.parent = ballSpawnLocation;
ball.transform.rotation = Quaternion.LookRotation(-Vector3.forward);
if (listener) {
GameObject.Find("StartGameButton").GetComponent<Button>().onClick.RemoveAllListeners();
listener = false;
}
}
I have recreated the menu so that the player inside the headset can access it and start the game. However, when the start button is pressed by the player, the coroutine does not run, and no balls spawn inside the throwers’ hands. This results in a null reference exception, as the script controlling the throwers attempts to reference the Rigidbody component attached to the ball prefab.
Rigidbody ballRigid = ball.GetComponent<Rigidbody>();
The ball never spawns, which also causes the throwers’ animation to activate, however it does not reset into the idle animation. I am currently attempting to have the coroutine run again as the Start Game button is pressed, however I am unsure how to approach it.
Relevant photos are attached to this link: https://drive.google.com/drive/folders/13hM3iZpskkl-dk72PT0IPac29lnswRCB?usp=sharing