I have a flashlight and I want it to always be index 0 when I GetChild() so it is always the first weapon I have selected. Is there a way to set it or does it always go alphabetically by the child name?
public int index;
List<string> weapons;
// Use this for initialization
void Start () {
weapons = new List<string>();
selectWeapon("Flashlight");
weapons.Add("Flashlight");
weapons.Add("Gun");
index = 0;
}
// Update is called once per frame
void Update () {
selectWeapon(weapons[index]);
if(Input.GetAxis("Mouse ScrollWheel") > 0) {
index++;
}
else if(Input.GetAxis("Mouse ScrollWheel") < 0) {
index--;
}
if(index > weapons.Count - 1) {
index = 0;
}
if(index < 0) {
index = weapons.Count - 1;
}
}
void selectWeapon(string tag) {
foreach(Transform weapon in transform) {
weapon.gameObject.SetActiveRecursively(tag == weapon.tag);
}
}
No, you should not trust in the children order, specially if you add or remove items. For weapon switching, the best way is to check the name or tag. A simple weapon switcher script using names could be like this:
function SwitchWeapon(name: String){
for (weapon: Transform in transform){
weapon.gameObject.SetActiveRecursively(name==weapon.name);
}
}
function Update(){
if (Input.GetKeyDown("0")) SwitchWeapon("Flashlight");
if (Input.GetKeyDown("1")) SwitchWeapon("Pistol");
if (Input.GetKeyDown("2")) SwitchWeapon("ShotGun");
... // and so on
}
A better way is to use tags instead of names. This helps acquiring weapons during the game, because you can use the same tag for the actual weapon and the model you pick up. The script above using tags and with the picking up code could be like this:
var hasPistol = false;
var hasShotGun = false;
function Start(){
SwitchWeapon("Flashlight"); // make sure only the basic weapon is active
}
function SwitchWeapon(tag: String){
for (weapon: Transform in transform){
// activate the selected weapon and deactivate all the others:
weapon.gameObject.SetActiveRecursively(tag==weapon.tag);
}
}
function Update(){
if (Input.GetKeyDown("0")) SwitchWeapon("Flashlight");
if (hasPistol && Input.GetKeyDown("1")) SwitchWeapon("Pistol");
if (hasShotGun && Input.GetKeyDown("2")) SwitchWeapon("ShotGun");
}
function OnTriggerEnter(other: Collider){
if (other.tag=="Pistol"){
hasPistol = true; // enable pistol and...
SwitchWeapon("Pistol"); // automatically select it
Destroy(other.gameObject); // destroy the fake weapon
}
if (other.tag=="ShotGun"){
hasShotGun = true; // enable shotgun and...
SwitchWeapon("ShotGun"); // automatically select it
Destroy(other.gameObject); // destroy the fake weapon
}
}
Append all the weapons to the player in the Editor - only the flashlight will be selected at first. The other weapons can only be selected if you’ve acquired them, which usually happens when you “pick up” the weapon - actually, a fake weapon model with the right tag and a trigger: when you enter its trigger, you enable the corresponding weapon and destroy the fake model.
NOTE: Usually the weapons are all children of an empty object, which in turn is childed to the player’s camera. Since empty objects don’t have colliders, they’re not detected by the weapons’ triggers, and the pickup code fails. A simple solution is to add this ridiculously small script to the player: it will redirect the trigger event to the weapon switcher:
function OnTriggerEnter(other: Collider){
BroadcastMessage("OnTriggerEnter", other, SendMessageOptions.DontRequireReceiver);
}