Gun shop - gun doesn't switch pleaseee help
Hello! I am trying to build a gun shop. on this script, everything is working, the gun itself (its properties i mean) is changing, but the model is not.. this is my script. i have set up the array of gameobjects with guns that have skinned mesh renderers attached to them, but now when i buy a gun, it doesn't delete the default gun (gun number 0), it just puts the gun that i bought next to it, without destroying the default gun. why is this? using UnityEngine; using System.Collections; using UnityEngine.UI;
public class WeaponButton : MonoBehaviour
{
public GameObject[] weaponObjects;
public PlayerShooting playerShooting;
public MoneyManager moneyManager;
public int weaponNumber;
public Image image;
// public Text name;
public Text cost;
//public Text description;
public Text alreadyHave;
public Text purchaseComplete;
public Text insufficient;
public int activeCount;
public Slider damageSlider;
public Slider timeSlider;
private AudioSource source;
// Use this for initialization
void Start()
{
moneyManager = GameObject.FindGameObjectWithTag("Money").GetComponent<MoneyManager>();
source = GetComponent<AudioSource>();
SetButton();
purchaseComplete.enabled = false;
alreadyHave.enabled = false;
SwitchWeapon(0);
}
void SetButton()
{
string costString = playerShooting.weapons[weaponNumber].cost.ToString();
//name.text = playerShooting.weapons[weaponNumber].name;
cost.text = "$" + playerShooting.weapons[weaponNumber].cost;
//description.text = playerShooting.weapons[weaponNumber].description;
timeSlider.maxValue = 0.7f;
timeSlider.value = timeSlider.maxValue - playerShooting.weapons[weaponNumber].fireRate;
damageSlider.value = playerShooting.weapons[weaponNumber].damage;
if (playerShooting.weapons[weaponNumber].doHaveit)
{
image.color = new Color32(98, 74, 139, 255);
}
}
public void BuyItem()
{
if(playerShooting.weapons[weaponNumber] == playerShooting.weapons[playerShooting.currentWeapon])
{
StartCoroutine(ShowAlreadyHave());
}
if (playerShooting.weapons[weaponNumber].doHaveit)
{
playerShooting.currentWeapon = weaponNumber;
SwitchWeapon(weaponNumber);
StartCoroutine(ShowAlreadyHave());
}
else if (moneyManager.totalMoney >= playerShooting.weapons[weaponNumber].cost && !playerShooting.weapons[weaponNumber].doHaveit)
{
moneyManager.totalMoney -= playerShooting.weapons[weaponNumber].cost;
playerShooting.currentWeapon = weaponNumber;
playerShooting.weapons[weaponNumber].doHaveit = true;
SwitchWeapon(weaponNumber);
StartCoroutine(ShowPurchase());
Social.ReportProgress("CgkInf3suvQWEAIQCw", 100.0f, (bool success) => { });
}
else if(moneyManager.totalMoney < playerShooting.weapons[weaponNumber].cost)
{
StartCoroutine(ShowInsufficient());
}
}
IEnumerator ShowAlreadyHave()
{
insufficient.enabled = false;
purchaseComplete.enabled = false;
alreadyHave.enabled = true;
yield return new WaitForSeconds(3);
alreadyHave.enabled = false;
}
IEnumerator ShowPurchase()
{
insufficient.enabled = false;
alreadyHave.enabled = false;
purchaseComplete.enabled = true;
yield return new WaitForSeconds(3);
purchaseComplete.enabled = false;
}
IEnumerator ShowInsufficient()
{
purchaseComplete.enabled = false;
alreadyHave.enabled = false;
insufficient.enabled = true;
yield return new WaitForSeconds(3);
insufficient.enabled = false;
}
private void Update()
{
if (playerShooting.weapons[weaponNumber].doHaveit)
{
image.color = new Color32(255, 255, 255, 255);
}
}
void SwitchWeapon(int index)
{
for (int i = 0; i < weaponObjects.Length; i++)
{
if (i == index)
{
weaponObjects[i].GetComponent<SkinnedMeshRenderer>().enabled = true;
}
else
{
weaponObjects[i].GetComponent<SkinnedMeshRenderer>().enabled = false;
}
}
}
}
Answer by sipSups · Feb 16, 2017 at 02:22 PM
PROBLEM FIXED - on my array, array item number 3 was empty, so it jumped but then got out of bounds.. something weird yeah.. this was the problem all along! Don't use EMPTY ARRAYS ON GAMEOBJECTS guys.
Your answer
![](https://koobas.hobune.stream/wayback/20220612111232im_/https://answers.unity.com/themes/thub/images/avi.jpg)