Good afternoon.
I have a problem with a series of buttons on world space canvas for my VR project. I have a global money object with an int and I have been trying to get the code to subtract “money” from that int in the global object when the canvas button is pressed and have it spawn a gun and ammo at the same time. I am not getting errors with the code, but it is not working at all. I am hoping someone can help me out.
I have script that I put on the button, then on the unity “on click()” instance I use the “gunPurchase()” function I made. as such:
public void gunPurchase()
{
if (gameObject.GetComponent().treasureAmount >= weaponPrice)
{
weaponPrice -= gameObject.GetComponent().treasureAmount;
AudioSource.PlayClipAtPoint(purchase, transform.position, 5);
randomInt = Random.Range(0, weaponAmmo.Length);
Instantiate(weaponAmmo[randomInt], spawnPos.position, spawnPos.rotation);
randomInt = Random.Range(0, WeaponAmmo2.Length);
Instantiate(WeaponAmmo2[randomInt], spawnPos2.position, spawnPos2.rotation);
}
else if (gameObject.GetComponent().treasureAmount < weaponPrice)
{
AudioSource.PlayClipAtPoint(denied, transform.position, 5);
}
}
If I take all the references to the global control and weapon price leaving only the audio source to play and the two spawn ins, it works great, but I cant seem to get to work so that it takes “money” away from that global variable. not to mention since the beginning does not seem to work, I cannot test to see if the lack of funds would play the denied audio source.
any and all help would be very appreciated as i am very stuck!
Thank you for your time and attention.