Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to spawn random object after clicking on a button

Discussion in 'Scripting' started by pilotrl, Jan 20, 2022.

  1. pilotrl

    pilotrl

    Joined:
    Jan 31, 2021
    Posts:
    55
    Hi all,
    I have a question I'm creating a game but I would make that when I click on a button it have to spawn a random object but my script doesn't work:
    I tried with an infinity loop but unity crash

    Code (CSharp):
    1.    void Start() {
    2.       StartSpawnFruits();
    3.    }
    4.  
    5.    void StartSpawnFruits() {
    6.       for (; ; ) {
    7.          var random = Random.Range(1, 3);
    8.  
    9.          if (ananasIsBought == true) {
    10.             switch (random) {
    11.                case 1:
    12.                   Instantiate(fruits[0], spawnFruitPos.position, Quaternion.identity);
    13.                   break;
    14.                case 2:
    15.                   Instantiate(fruits[1], spawnFruitPos.position, Quaternion.identity);
    16.                   break;
    17.             }
    18.  
    19.             Instantiate(fruits[random], spawnFruitPos.position, Quaternion.identity);
    20.             StartCoroutine(SpawnFruitsTimer());
    21.            
    22.          }
    23.          else if (ananasIsBought == false) {
    24.             for (int i = 0; i < 1; i++) {
    25.                Instantiate(fruits[i], spawnFruitPos.position, Quaternion.identity);
    26.                StartCoroutine(SpawnFruitsTimer());
    27.             }
    28.          }
    29.       }
    30.    }
    31.  
    32.    IEnumerator SpawnFruitsTimer() {
    33.       yield return new WaitForSeconds(1);
    34.       StartSpawnFruits();
    35.    }
    Any suggestion Thanks
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What you ask for is not hard, however "does not work" can mean literally anything. When you have a problem, you usually want to describe what the desired behavior is and what's happening instead.
    You also want to post the entire script, since now i have to guess what type things are.

    You said you want to spawn a random object when a button is pressed. And i assume you intend to call that function when said button is pressed, because currently you are doing so in Start(). However, that does not seem to be all you want to do. What is that about Ananas?

    Based on the information provided and assuming 'fruits' contains prefabs, this is all what you want:
    Code (CSharp):
    1. int random = Random.Range(0, fruits.Count); // .Length for arrays
    2. Instantiate(fruits[random], spawnFruitPos.position, Quaternion.identity);
    Some general feedback on code quality:
    • You dont need to compare bools to true or false. someBool == true is the same as just someBool. And someBool == false is the same as !someBool (read: not someBool)
    • Bools are binary. Your ananasIsBought is either true or false. There is no other option. So if your if(ananasIsBought) fails, the bool must be false. Thus you do not need to put an "else if" there to check that.
    • Id recommend against using 'var'. It only leads to bad habits. There can be a hand ful of usecases where the var keyword might be actually useful, but in general it is just lazy and will, if overused, make maintenance of your code a whole lot harder. You generally want to know what type things are.
    • The second for loop only runs one iteration, meaning it might as well be deleted.
     
    pilotrl likes this.
  3. pilotrl

    pilotrl

    Joined:
    Jan 31, 2021
    Posts:
    55
    Many Many Thanks for your help and suggestion yet I work Many Thanks