Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Mar 26, 2018 at 12:12 AM · instantiateshootinglistsrandom.range

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

146 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges