Complete the code to declare a public GameObject variable named playerPrefab.
public class GameManager : MonoBehaviour { public [1] playerPrefab; }
The playerPrefab must be a GameObject to spawn the player in Unity.
Complete the code to instantiate the playerPrefab at the origin with no rotation.
void SpawnPlayer() {
Instantiate(playerPrefab, [1], Quaternion.identity);
}Vector3.zero represents the origin point (0,0,0) where the player should spawn.
Fix the error in the code to correctly spawn the playerPrefab inside Start().
void Start() {
[1](playerPrefab, Vector3.zero, Quaternion.identity);
}Instantiate is the correct method to create a new instance of a prefab in Unity.
Fill both blanks to spawn the playerPrefab at a random position on the XZ plane with Y=0.
void SpawnRandomPlayer() {
Vector3 randomPos = new Vector3(Random.Range([1], [2]), 0, Random.Range(-10, 10));
Instantiate(playerPrefab, randomPos, Quaternion.identity);
}The X position should be between -10 and 10 to spawn randomly on the X axis.
Fill all three blanks to spawn the playerPrefab with a random rotation around the Y axis.
void SpawnPlayerWithRotation() {
Quaternion randomRotation = Quaternion.Euler(0, [1], 0);
Vector3 spawnPos = new Vector3([2], 0, [3]);
Instantiate(playerPrefab, spawnPos, randomRotation);
}The Y rotation is random between 0 and 360 degrees. The spawn position is at (0,0).