I’m learning Unity and trying to wrap my brain around a script to change weapons. I’m using an HTC Vive but I don’t think this matters so much. I’ll try to be as detailed as I can.
This is the SteamVR Camera Rig. It has two Controllers.
Model
is the built in one that I have disabled for now. MyMachinePistol
and Pistol 1
are the two models I want to swap between, both have the tag Gun
.
I have the following script so far:
using UnityEngine;
using System.Collections;
public class ChangeWeaponScript : MonoBehaviour {
private GameObject[] guns;
void Start () {
GetComponent<SteamVR_TrackedController>().PadClicked += new ClickedEventHandler(PadClicked);
guns = GameObject.FindGameObjectsWithTag ("Gun");
}
void PadClicked(object sender, ClickedEventArgs e)
{
Debug.Log ("pad clicked");
//Put code here to disable the current model and enable the next
//Something like guns[0].SetActive(false); guns[1].SetActive(true);
}
}
The PadClicked
function fires fine. My problem is if I disable one of the models (to make the other one the starting model) .FindGameObjectsWithTag
won’t put the disabled one into the array because it can’t see disabled object.
So then I thought I would just put the gun models into an empty gameobject and hide them somewhere off screen, but then I don’t know how to move/remove them from Controller (right)
in code.
Is there a better way to do this that I, as a beginner, haven’t seen?