你好我试图制造一个枪系统,一切正常,除了存储在我的GameObject阵列playerGuns中的切换枪,它总是将它存储在0 当我按下另一个键时,它会禁用所有创建的对象。
我想这样做,当你按下键盘上的1-4键时它会切换 在存储在playerGuns上的枪和禁用另一把枪的枪之间,你持有。
我无法找到问题发生的原因所以请帮忙。
public GameObject Player;
public GameObject Showui;
public RectTransform Uirect;
public Material greenQu, blueQu;
public GameObject nameObject;
public GameObject rearityObject;
public GameObject dmgObject;
public GameObject levelObject;
private Text nameText;
private Text rearityText;
private Text dmgText;
private Text levelText;
private Image infoImage;
private Gunsystem gunScript;
private string gunName;
private int index = 0, index2 = 0;
private bool pressed = false, Show = false;
public Gun[] Inventory = new Gun[4];
public GameObject[] playerGuns = new GameObject[4] { null, null, null, null};
public int keyPress = 0;
void Start () {
gunScript = Player.GetComponent<Gunsystem>();
infoImage = Showui.GetComponent<Image>();
//Get text from objects
nameText = nameObject.GetComponent<Text>();
rearityText = rearityObject.GetComponent<Text>();
dmgText = dmgObject.GetComponent<Text>();
levelText = levelObject.GetComponent<Text>();
gunName = gameObject.name;
Debug.Log(gunName);
}
private void Update()
{
KeyCode pickup = KeyCode.E;
KeyCode key1 = KeyCode.Alpha1;
KeyCode key2 = KeyCode.Alpha2;
KeyCode key3 = KeyCode.Alpha3;
KeyCode key4 = KeyCode.Alpha4;
//Handling inventory keys
if (Input.GetKeyDown(key1))
{
keyPress = 0;
if (playerGuns[0] != null && !playerGuns[0].activeInHierarchy)
{
playerGuns[0].SetActive(true);
}
if (playerGuns[1] != null && playerGuns[1].activeInHierarchy)
{
playerGuns[1].SetActive(false);
}else if (playerGuns[2] != null && playerGuns[2].activeInHierarchy)
{
playerGuns[2].SetActive(false);
}
else if (playerGuns[3] != null && playerGuns[3].activeInHierarchy)
{
playerGuns[3].SetActive(false);
}
}else if (Input.GetKeyDown(key2))
{
keyPress = 1;
if (playerGuns[1] != null && !playerGuns[1].activeInHierarchy)
{
playerGuns[1].SetActive(true);
}
if (playerGuns[0] != null && playerGuns[0].activeInHierarchy)
{
playerGuns[0].SetActive(false);
}
else if (playerGuns[2] != null && playerGuns[2].activeInHierarchy)
{
playerGuns[2].SetActive(false);
}
else if (playerGuns[3] != null && playerGuns[3].activeInHierarchy)
{
playerGuns[3].SetActive(false);
}
}else if (Input.GetKeyDown(key3))
{
keyPress = 2;
if (playerGuns[2] != null && !playerGuns[2].activeInHierarchy)
{
playerGuns[2].SetActive(true);
}
if (playerGuns[0] != null && playerGuns[0].activeInHierarchy)
{
playerGuns[0].SetActive(false);
}
else if (playerGuns[1] != null && playerGuns[1].activeInHierarchy)
{
playerGuns[1].SetActive(false);
}
else if (playerGuns[3] != null && playerGuns[3].activeInHierarchy)
{
playerGuns[3].SetActive(false);
}
}
else if (Input.GetKeyDown(key4))
{
keyPress = 3;
if (playerGuns[3] != null && !playerGuns[3].activeInHierarchy)
{
playerGuns[3].SetActive(true);
}
if (playerGuns[0] != null && playerGuns[0].activeInHierarchy)
{
playerGuns[0].SetActive(false);
}
else if (playerGuns[1] != null && playerGuns[1].activeInHierarchy)
{
playerGuns[1].SetActive(false);
}
else if (playerGuns[2] != null && playerGuns[2].activeInHierarchy)
{
playerGuns[2].SetActive(false);
}
}
//Pick the gun when key pressed and store it
if (Input.GetKeyDown(pickup) && Show == true)
{
foreach (Gun item in gunScript.gunList)
{
if (index2 == Convert.ToInt32(gunName))
{
if (item.gunType == "Normal")
{
GameObject temp = GameObject.Find(gameObject.name);
playerGuns[keyPress] = Instantiate(temp, Player.transform.position, Quaternion.identity) as GameObject;
playerGuns[keyPress].name = gameObject.name;
playerGuns[keyPress].tag = gameObject.tag;
playerGuns[keyPress].transform.parent = Player.transform;
playerGuns[keyPress].transform.rotation.SetLookRotation(Player.transform.position);
//Change rigidbody and disable collider
playerGuns[keyPress].GetComponent<Rigidbody>().useGravity = false;
playerGuns[keyPress].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
playerGuns[keyPress].GetComponent<MeshCollider>().enabled = false;
//Store gundata in inventory
item.Named = false;
Inventory[keyPress] = item;
pressed = true;
}
else if (item.gunType == "Stride")
{
GameObject temp = GameObject.Find(gameObject.name);
playerGuns[keyPress] = Instantiate(temp, Player.transform.position, Quaternion.identity) as GameObject;
playerGuns[keyPress].name = gameObject.name;
playerGuns[keyPress].tag = gameObject.tag;
playerGuns[keyPress].transform.parent = Player.transform;
playerGuns[keyPress].transform.rotation.SetLookRotation(Player.transform.position);
//Set rgidbody and disable collider
playerGuns[keyPress].GetComponent<Rigidbody>().useGravity = false;
playerGuns[keyPress].GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
playerGuns[keyPress].GetComponent<MeshCollider>().enabled = false;
//Store gundata in inventory
item.Named = false;
Inventory[keyPress] = item;
pressed = true;
}
}
index2++;
}
index2 = 0;
}
}
private void OnMouseEnter()
{
Vector3 Pos = Camera.main.WorldToScreenPoint(transform.position);
Showui.transform.position = Pos;
Showui.transform.position += new Vector3(0, 90f, 0.5f);
gunScript.updateGuns = true;
Show = true;
foreach (Gun item in gunScript.gunList)
{
if (index == Convert.ToInt32(gunName))
{
if(item.gunType == "Normal")
{
infoImage.material = greenQu;
}else if (item.gunType == "Stride")
{
infoImage.material = blueQu;
}
LayoutRebuilder.ForceRebuildLayoutImmediate(Uirect);
if(item.Named != false)
Showui.SetActive(true);
nameText.text = "Name: " + item.Name;
rearityText.text = "Rearity: " + item.gunType;
dmgText.text = "Damage: " + Mathf.Round(item.Dmg).ToString();
levelText.text = "Level: " + item.gunLevel.ToString();
//Debug.Log(item.Name);
//Debug.Log(item.gunType);
//Debug.Log(item.Dmg);
//Debug.Log(item.gunLevel);
}
index++;
}
index = 0;
}
private void OnMouseExit()
{
Show = false;
Showui.SetActive(false);
}
答案 0 :(得分:4)
我写了一个小脚本,应该处理你的四支枪的激活和停用。
希望它有所帮助,
Alex
public GameObject[] Guns;
void Start(){
for (int i=0; i<3; i++){
if(Guns[i] == null) Debug.LogError("Gun n°" + i +" is null);
}
}
void Update(){
if(Input.GetKeyDown(Input.KeyCode.Alpha1)){
setGunActive(1);
}
if(Input.GetKeyDown(Input.KeyCode.Alpha2)){
setGunActive(2);
}
if(Input.GetKeyDown(Input.KeyCode.Alpha3)){
setGunActive(3);
}
if(Input.GetKeyDown(Input.KeyCode.Alpha4)){
setGunActive(4);
}
}
void setGunActive(int n){
foreach(GameObject g in Guns){
g.setActive(false);
}
Guns[n].setActive(true);
}