Question by
George_Codes · Jan 01, 2021 at 11:23 PM ·
gundisablebreakrandomize
removing an int after random use
Yeah idk how to work with break or return but for my semi randomized game i need it to pick out a random number do it then disable it, to reload is pressed(i already have the randomized and doing stuff working)her it is. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RandomizedGun : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform gunbody;
public GameObject hiteffect;
public Transform player;
public GameObject medkitPrefab;
public Transform Mag;
public float bulletForce = 20f;
System.Random rand = new System.Random();
int gun;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
gun = rand.Next(1, 7);
if (gun == 1)
{
Shoot1();
}
else if (gun == 2)
{
Shoot1();
}
else if (gun == 3)
{
Shoot1();
}
else if (gun == 4)
{
Shoot2();
}
else if (gun == 5)
{
Shoot1();
}
else if (gun == 6)
{
Shoot3();
}
}
}
void Shoot1()
{
GameObject bullet = Instantiate(bulletPrefab, gunbody.position, gunbody.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(gunbody.up * bulletForce, ForceMode2D.Impulse);
}
void Shoot2()
{
GameObject effect = Instantiate(hiteffect, Mag.position, Quaternion.identity);
Destroy(effect, 1f);
}
void Shoot3()
{
GameObject medkit = Instantiate(medkitPrefab, gunbody.position, gunbody.rotation);
Rigidbody2D rb2 = medkit.GetComponent<Rigidbody2D>();
rb2.AddForce(gunbody.up * bulletForce, ForceMode2D.Impulse);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613022527im_/https://answers.unity.com/themes/thub/images/avi.jpg)