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 /
avatar image
0
Question by florivault · Oct 18, 2017 at 05:12 PM · random spawn

Instantiate a minimum of gameObject (instantiate un nombre minimum de gameObject)

hi everybody, i'm a begginer and i tried to spawn item at random spots. but i need to spawn a minimum of gameObject, like 7. actually, it spawns a random number of gameObject.

here is my code to spawn :

void Start() {

     for (int i = 0; i < spawnPosition.Length; i++){

         int itemToCreate = Random.Range(0, 3);
         if (itemToCreate == 0) {
             itemInScene.Add(Instantiate(diamond, spawnPosition[i].position, Quaternion.identity));
         }
     }

     scoreCount = 0;
     level = 1;

     highScoreText.text = PlayerPrefs.GetInt("BEST").ToString();
 }

 public void MoreDiamond (){
     bool checkItemInScene = true;
     for (int i = 0; i < spawnPosition.Length; i++){

         int itemToCreate = Random.Range(0, 10);
         if (itemToCreate >= 5) {

             tempDiamond = (GameObject)Instantiate (diamond, spawnPosition [i].position, Quaternion.identity);
             tempDiamond.transform.name = "Diamond_" + Random.Range (0, 100).ToString("000"); 

             tempDiamond.GetComponent<Diamond> ().DiamondUpgrade (level);
             itemInScene.Add(tempDiamond);
             diamondScript.Add(tempDiamond.GetComponent<Diamond>());
         

         } else if (itemToCreate == 1 && checkItemInScene) {
             itemInScene.Add (Instantiate (itemBall, spawnPosition [i].position, Quaternion.identity));    
             checkItemInScene = false;//<- instantiate autre chose
         }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by marcrem · Oct 18, 2017 at 05:42 PM

You are currently randomly choosing wether you're spawning an object or not. If I understand correctly, you should instead have an int called minimumNumberOfObjects or something like that, then :

      for (int i = 0; i < minimumNumberOfObjects; i++){
              itemInScene.Add(Instantiate(diamond, spawnPosition[i].position, Quaternion.identity));
 
      }

Using this, your FOR loop will go until it reached your magical minimumNumberOfObjects int.

Comment
Add comment · Show 3 · Share
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
avatar image marcrem · Oct 18, 2017 at 05:44 PM 1
Share

And if you want them to choose the spawnPosition randomly, then you do this :

       for (int i = 0; i < $$anonymous$$imumNumberOfObjects; i++){
 int pickedPosition = Random.Range(0, spawnPosition.Length);              
 itemInScene.Add(Instantiate(diamond, spawnPosition[pickedPosition].position, Quaternion.identity));
  
       }
avatar image marcrem · Oct 18, 2017 at 06:06 PM 0
Share

P.S. Je parle français ;)

avatar image florivault marcrem · Oct 18, 2017 at 08:01 PM 0
Share

salut et merci de ton aide en faite, ce que je veux c'est controler le nombre exact de "diamond" qui vont spawn.

je comprend ce que tu as ecrit mais je vois absolument pas ou l'ecrire dans ma methode "more diamond"...

avatar image
1

Answer by nenok1 · Oct 18, 2017 at 06:47 PM

         // calculate amount of spawns
         int minimumSpawns = 7;
         int maximumSpawns = 10;
         int amountOfSpawns = Random.Range(minimumSpawns, maximumSpawns);
 
         // instantiate objects at random positions
         for (int i = 0; i < amountOfSpawns; i++) {
             int randomPositionIndex = Random.Range(0, spawnPosition.Length);
             itemInScene.Add(Instantiate(
                     diamond, 
                     spawnPosition[randomPositionIndex].position, 
                     Quaternion.identity));
         }
Comment
Add comment · Share
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

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

72 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

Related Questions

Random obstacles spawn in 2D side endless runner game ? 2 Answers

Random Object Spawning 0 Answers

Spawning enemies javascript 2 Answers

How to randomly instantiate an object at specific locations? 1 Answer

Multiple spawn location with random game object in random time spawn c# 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