How to make the gun disappear in the same key that makes it appear in UNITY

Asked

Viewed 105 times

2

   using UnityEngine;
   using System.Collections;

   public class SelecteArma : MonoBehaviour {


    public bool Equipamento = true;

    void Start () {
    }


    void Update () {
    if (Input.GetKeyDown ("1")) {
    selecionarArma (0);
    }

    if (Input.GetKeyDown ("2")) {
    selecionarArma (1);
    }
    }


    public void selecionarArma (int index){
    for (var i = 0; i<transform.childCount; i++) {
    if (i == index)
    transform.GetChild (i).gameObject.SetActiveRecursively (false);
    else
    transform.GetChild (i).gameObject.SetActiveRecursively (true);
    }
    }
    }

1 answer

3

You can simply assign a bool variable to its negation and use it to hide and show the weapon.

var muda = true;

//atribui o valor contrário ao actual, se for true passa para false e vice-versa
muda = !muda; 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.