Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawn randomly colored objects

Discussion in '2D' started by mham3d, Jan 17, 2018.

  1. mham3d

    mham3d

    Joined:
    Jan 17, 2018
    Posts:
    3
    Hello everyone, trying to make a game, i need to make a spawner spawns objects in four colors randomly when we click Fire1 button, in limit number for example 4 times red for time blue.. Etc but randomly and the spawner shows the coming color or the next color, i tried many things but didn't work for me. Any help plz.
     
  2. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    Low-tech solution, just create an array with the number of elements you need, such as:

    Code (CSharp):
    1. string[] colors = { "blue", "red", "red", "red", "red", "yellow", "yellow" };
    Then randomly choose something from that array -- you'll get red 4 times more than blue, and you'll get yellow half as much as red, etc.

    Jay
     
  3. mham3d

    mham3d

    Joined:
    Jan 17, 2018
    Posts:
    3
    thank you sir!