(VR) Switch Player Prefabs on Load Scene
I'm using SteamVR to make some VR scenes. I have a menu scene with a button setup to choose between a sword or shield. A play button then links to a new scene.
The menu scene has a unique player prefab to interact with the menu and I need to load a new player prefab that's either a shield_prefab or a sword_prefab depending on the selection.
I'm using a switch to store the decision and am currently using that to instantiate either prefab depending on the stored value from the menu screen.
My first problem was that the Menu Player prefab wasn't deleted when I loaded the new scene. So I put in a Destroy Object on the 2nd scene ans targeted the Menu Player prefab. This is destroying the Menu Player, but is now not properly loading the headset into the new player prefab.
Here's the code I'm using to call the prefab at the start.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetMainCharacter : MonoBehaviour
{
public GameObject SwordPlayer = null;
public GameObject ShieldPlayer = null;
private readonly string selectedCharacter = "SelectedCharacter";
private void Awake()
{
Destroy (GameObject.Find("Menu Player"));
}
// Start is called before the first frame update
void Start()
{
int getCharacter;
getCharacter = PlayerPrefs.GetInt(selectedCharacter);
switch(getCharacter)
{
case 1:
Instantiate(ShieldPlayer, transform.position, Quaternion.identity);
break;
case 2:
Instantiate(SwordPlayer, transform.position, Quaternion.identity);
break;
default:
Instantiate(ShieldPlayer, transform.position, Quaternion.identity);
break;
}
}
// Update is called once per frame
void Update()
{
}
}
Thanks for any help you can offer!
Answer by seanothe3rd · Sep 08, 2020 at 08:22 AM
Got it figured out. Needed to uncheck the do not destroy box on the steam VR Player prefab
Your answer
![](https://koobas.hobune.stream/wayback/20220613011435im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
References to Prefab get lost on start 1 Answer
Problem with instantiating objects using playerprefs when switching scenes 0 Answers
Changing eye height with steamVR Camera Rig 2 Answers
HTC Vive Pro crashes Unity, Steam VR reads "(unresponsive) Unity.exe" 0 Answers
(XR Input) Why does Oculus Rift work fine, but HTC Vive inputs stays at 0 ? 0 Answers