Upgrade Weapon Stats on Different Script
Hello! Okay, I dont know how to ask, so Ill just explain;
I want to be able to upgrade my player gun with a simple button press on an UI canvas. I already have the button, and the menu, but here is the thing:
My gun works with two different scripts. The first script is attached to the gun and spawns the bullet prefab, if enough ammo is there (thanks to a fellow unity-user Hellium for helping out! :))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public GameObject magazineDrop;
public Transform magazinePoint;
public GameObject Muzzle;
//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)
{
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()
{
gameObject.GetComponent<Animator>().SetTrigger("Muzzle");
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}
Then, the second script is on the bullet prefab. It handles a public int for the damage as well the hit detection:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullets : MonoBehaviour
{
public float speed = 99f; //Wie schnell ist die Kugel?
public int damage = 30;
public Rigidbody2D rb;
private void Start()
{
rb.velocity = transform.right * speed;
}
void OnTriggerEnter2D(Collider2D hitInfo)
{
Debug.Log(hitInfo.name);
Enemy enemy = hitInfo.GetComponent<Enemy>();
Destroyed todestroy = hitInfo.GetComponent<Destroyed>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
if (todestroy != null)
{
todestroy.TakeDamage(damage);
}
Destroy(gameObject);
}
}
I tried instanciating, but brackeys tut didnt work for me, since I got some scripts from my university and I cant change them (bullet, gun etc. was done by me, so I kinda understand what these do). There is also already a collectable-system in place, but i cant change that.
Basically, I want to be able to collect 6 coins, open up the menu and press the button and the damage output (the int I guess) will be higher, and 6 coins will be gone.
Can someone point me in the direction or help me out a bit more? I work on the UpgradeMenu for 2 days now and I dont get it. And its not helping that I cant acces certain scripts, so maybe, even if its not performance or logistic-wise good, a completely new script that handles this?
Thanks in advance! :)
Your answer
Follow this Question
Related Questions
Button Displacement 0 Answers
Button.OnClick Only Works Once. 0 Answers
Can't disable TMPro Buttons 1 Answer
Camera ProjectionMatrix and Canvas Scaling - different results in 2018.3 0 Answers