Change Function On Button Press?
Heyho!
I wanted to change my called function in my script, once the player hits a button in a menu. I was trying it with If and Else, but somehow I cant wrap my head around how to actually write it. Could somebody help me here? (Thanks Hellium for the Ammo help!) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Gun : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public GameObject bulletDPUPPrefab;
public GameObject magazineDrop;
public Transform magazinePoint;
public GameObject Muzzle;
public Button upgradeDamageButton;
//Destroy Munitionsdump
private Queue<GameObject> droppedMagazines = new Queue<GameObject>();
//Munitionsangaben
[SerializeField]
private GameObject[] ammo;
private int ammoAmount;
void Start()
{
ammoAmount = 0;
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && ammoAmount > 0)
{
If(upgradeDamageButton.onClick.AddListener)
{
ShootUpgrade();
ammoAmount -= 1;
ammo[ammoAmount].gameObject.SetActive(false);
}
else
{
Shoot();
ammoAmount -= 1;
ammo[ammoAmount].gameObject.SetActive(false);
}
}
if (Input.GetKeyDown(KeyCode.R))
{
droppedMagazines.Enqueue(Instantiate(magazineDrop, magazinePoint.position, firePoint.rotation));
if (droppedMagazines.Count > 5)
Destroy(droppedMagazines.Dequeue());
ammoAmount = 6;
for (int i = 0; i <= 5; i++)
{
ammo[i].gameObject.SetActive(true);
}
}
}
void Shoot()
{
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
public void ShootUpgrade() //So, wir haben eine Function, die vom Button gecalled werden kann, da public
{
Instantiate(bulletDPUPPrefab, firePoint.position, firePoint.rotation); //Hier wird nun ein anderes Prefab instanziert
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I make a credits button? 1 Answer
Missing function in button on click 0 Answers
There's an issue with this script I can't find 0 Answers
Button Displacement 0 Answers
Function will only set layer in some cases, Why is this? 1 Answer