Cannot pick random GameObject from list.
First of all, code (i can't be bothered to clean it up so that you can understand it better so bear with me): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PhsyicsGun : MonoBehaviour {
//GameObjects
public GameObject MainBullet;
GameObject CurrentBullet;
public GameObject player;
GameObject bulletPool;
// private GameObject[] bulletsArray = new GameObject[500];
//Floats
public float bulletSpeed;
public float aimSpeed;
public float resetSpeed;
float defaultSpreadForce;
float randomFloat;
public float hipFireInaccuracy;
public float spreadForce;
[Range(0.001f, 1f)]
public float delay;
//Vector3s
public Vector3 hipFire;
public Vector3 aim;
//Bools
bool isAiming;
bool slowMo;
//Rigidbodies
Rigidbody bulletRb;
Rigidbody CurrentBulletRb;
//Strings
string bulletName;
//Ints
int rnd;
int index;
public int preInstatiatedBullets;
//ToTidy
List<GameObject> bulletsList = new List<GameObject>();
//
//GameObject currentBullet;
void Start(){
defaultSpreadForce = spreadForce;
Random.InitState(Random.seed = System.DateTime.Now.Millisecond);
Setup();
PoolBullets();
StartCoroutine(PooledShoot());
}
void Setup() {
MainBullet = GameObject.FindGameObjectWithTag("MainBullet");
player = GameObject.FindGameObjectWithTag("Player");
bulletPool = GameObject.FindGameObjectWithTag("BulletPool");
bulletRb = MainBullet.GetComponent<Rigidbody>();
}
void Update(){
Aim();
Inaccuracy();
Debug_();
Cursor.visible = false;
}
void Debug_() { //Not relevant to issue
if (Input.GetKey(KeyCode.Y))
{
//Debug.Log(bulletsList.Count + " " + bulletsList[(Random.Range(0,5000))]);
Debug.Log(Time.timeScale);
}
if (Input.GetKeyDown(KeyCode.L))
{
print(slowMo);
slowMo = !slowMo;
if (slowMo)
{
Time.timeScale = 0.2f;
} else
{
Time.timeScale = 1;
}
}
}
void PoolBullets() { //Called once on startup, creates bullets and hides them for later use.
for (int a = 0; a < preInstatiatedBullets; a++) {
Instantiate(MainBullet);
MainBullet.transform.position = bulletPool.transform.position;
rnd = Random.Range(0, (Random.Range(100,10000)));
MainBullet.name = rnd.ToString();
bulletsList.Add(MainBullet);
MainBullet.SetActive(false);
}
}
/*
IEnumerator Shoot(){ //Not relevant to issue (old shoot method, causes lag when shooting).
while (true) {
if(Input.GetMouseButton(0)){
//print ("Coroutine executed");
bullet = GameObject.FindGameObjectWithTag ("BulletO");
bulletRb = bullet.GetComponent<Rigidbody> ();
Instantiate (bullet);
bullet.transform.position = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
bulletRb.velocity = transform.forward * bulletSpeed;
bullet.transform.rotation = player.transform.rotation;
randomFloat = Random.value;
if (randomFloat > 0 && randomFloat < 0.25f) {
bulletRb.AddForce (transform.up * (spreadForce * Random.value));
bulletRb.AddForce (transform.right * (spreadForce * Random.value));
//UpRight
} else if (randomFloat > 0.25f && randomFloat < 0.5f) {
bulletRb.AddForce (-transform.up * (spreadForce * Random.value));
bulletRb.AddForce (transform.right * (spreadForce * Random.value));
//DownRight
} else if (randomFloat > 0.5f && randomFloat < 0.75f) {
bulletRb.AddForce (-transform.right * (spreadForce * Random.value));
bulletRb.AddForce (-transform.up * (spreadForce * Random.value));
//DownLeft
} else {
bulletRb.AddForce (-transform.right * (spreadForce * Random.value));
bulletRb.AddForce (transform.up * (spreadForce * Random.value));
//UpLeft
}
// bullet.tag = "Null";
}
yield return new WaitForSeconds(delay);
}
}
*/
IEnumerator PooledShoot() {
while (true)
{
if (Input.GetMouseButton(0))
{
PickBullet();
CurrentBullet.SetActive(true);
CurrentBullet.transform.position = transform.position;
CurrentBullet.transform.rotation = player.transform.rotation;
CurrentBulletRb.velocity = transform.forward * bulletSpeed;
BulletSpread();
print(CurrentBullet + " " + CurrentBulletRb);
CurrentBullet = null;
CurrentBulletRb = null;
}
yield return new WaitForSeconds(delay);
}
}
void PickBullet() {
index = Random.Range(0, preInstatiatedBullets);
//Debug.Log(index);
CurrentBullet = bulletsList[index];
CurrentBulletRb = CurrentBullet.GetComponent<Rigidbody>();
CurrentBullet.tag = "CurrentBullet";
CurrentBullet.name = "CurrentBullet";
Debug.Log(CurrentBullet + "Picked");
}
void Aim() //Not relevant to issue
{
if (Input.GetMouseButton(1))
{
transform.localPosition = Vector3.Slerp(transform.localPosition, aim, aimSpeed * Time.deltaTime);
isAiming = true;
}
else
{
transform.localPosition = Vector3.Slerp(transform.localPosition, hipFire, resetSpeed * Time.deltaTime);
isAiming = false;
}
}
void BulletSpread() //Not relevant to issue
{
randomFloat = Random.value;
if (randomFloat > 0 && randomFloat < 0.25f)
{
bulletRb.AddForce(transform.up * (spreadForce * Random.value));
bulletRb.AddForce(transform.right * (spreadForce * Random.value));
//UpRight
}
else if (randomFloat > 0.25f && randomFloat < 0.5f)
{
bulletRb.AddForce(-transform.up * (spreadForce * Random.value));
bulletRb.AddForce(transform.right * (spreadForce * Random.value));
//DownRight
}
else if (randomFloat > 0.5f && randomFloat < 0.75f)
{
bulletRb.AddForce(-transform.right * (spreadForce * Random.value));
bulletRb.AddForce(-transform.up * (spreadForce * Random.value));
//DownLeft
}
else
{
bulletRb.AddForce(-transform.right * (spreadForce * Random.value));
bulletRb.AddForce(transform.up * (spreadForce * Random.value));
//UpLeft
}
}
void Inaccuracy() //Not relevant to issue
{
if (!isAiming)
{
if (spreadForce < defaultSpreadForce + hipFireInaccuracy)
{
spreadForce += hipFireInaccuracy;
}
}
else
{
spreadForce = defaultSpreadForce;
}
}
}
So, to make it short, i this script to create N bullets at runtime, deactivates them and puts them in a hidden place (Bullet pool) and adds them to a list (bulletsList). When i press Mouse0 i want it to pick one bullet from the list, activate it, move it to the gun and shoot it, but when i press mouse0 it takes the main bullet (the one that already exists) and uses that one instead, and everytime PooledShoot is called the same bullet is taken and put back into the gun, instead of picking one from the list. i've tried every solution humanly possible, but the script keeps picking the same exact bullet even tho i've told it to pick a Random.Range(0,10) from the list.
It has been a week since i've encountered this problem but i can't fix it, i'm frustrated.
Your answer
Follow this Question
Related Questions
Random position of asteroids -- Random not randomizing? 1 Answer
Instantiate a prefab at three specific x positions 0 Answers
List or Arrays, and how to randomize them. 1 Answer
The bullet hole colliding with player and "Triggers" 0 Answers
i have a list of transforms and i want to spawn a prefab once for all the transforms in the list. 1 Answer