how to make % posibility to spawn an object C# in unity

 if(Random.value > 0.5) //%50 percent chance {//code here }  if(Random.value > 0.2) //%80 percent chance (1 - 0.2 is 0.8) { //code here }  if(Random.value > 0.7) //%30 percent chance (1 - 0.7 is 0.3) { //code here }

3.75
4
Phoenix Logan 186120 points

                                     public class RandomTimeSpawner : MonoBehaviour {      //Spawn this object     public GameObject spawnObject;      public float maxTime = 5;     public float minTime = 2;      //current time     private float time;      //The time to spawn the object     private float spawnTime;      void Start(){         SetRandomTime();         time = minTime;     }      void FixedUpdate(){          //Counts up         time += Time.deltaTime;          //Check if its the right time to spawn the object         if(time >= spawnTime){             SpawnObject();             SetRandomTime();         }      }       //Spawns the object and resets the time     void SpawnObject(){         time = minTime;         Instantiate (spawnObject, transform.position, spawnObject.transform.rotation);     }      //Sets the random time between minTime and maxTime     void SetRandomTime(){         spawnTime = Random.Range(minTime, maxTime);     }  } 

3.75 (4 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source