Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 The_Unity_Game_Developer · Jan 12, 2015 at 10:15 PM · randominstantiate prefab

How can instantiate in set random places?

Hello!

As the question states, I need to instantiate in random places. I know that I have asked a question like this before, but I felt that this should have its own question.

I have used some code from answers in that previous question, and here is the current code:

 #pragma strict
  
 var choosingIsActive : boolean = true;
 public var spawnPoints : Transform [];
 public var prefab : GameObject;
 var standByCamera : GameObject;
 var playerCamera : GameObject;
 var monsterPrefab : Transform;
  
  function Start () {
  
  }
  
  function Update () {
      
  }
  
  public function OnGUI()
    {
        if(choosingIsActive)
         {
         
             if(GUI.Button(Rect(Screen.width/2.1, Screen.height/2.1, 106, 50), "Play!"))
             {
                     choosingIsActive = false;
                     standByCamera.SetActive(false);
                     playerCamera.SetActive(true);
              }
              
              if(GUI.Button(Rect(Screen.width/2.1, Screen.height/1.8, 106, 50), "10 Monsters"))
              {
              var indx: int = Random.Range(0,spawnPoints.Length-1);
              var spawnPosition : Vector3 = spawnPoints[indx].position;
                  for (var i : int = 0; i > 10; i++) {
                      Instantiate(monsterPrefab, spawnPosition, Quaternion.identity);
                  }
              }
         }
    }

I don't get any errors, but when I press on the 30 Monsters button nothing happens. Even when I press Play there is nothing there. I look in the Hierarchy and there is nothing added to it.

I have searched Google an d Unity Answers to try and find help, but I haven't been able to find anything.

Can anybody help me please?

Thanks in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by InvincibleCat · Jan 12, 2015 at 10:25 PM

it's i < 10 and no i > 10 (and 10 should be a variable representing the number of monsters you want to instantiate) And if you want your monsters to spawn at different positions, you should do

               var indx: int = Random.Range(0,spawnPoints.Length-1);
               var spawnPosition : Vector3 = spawnPoints[indx].position;

in your for loop

Cheers

Comment
Add comment · Show 11 · 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 The_Unity_Game_Developer · Jan 12, 2015 at 11:07 PM 0
Share

Ok thanks. But one more thing. How do I define the variable for 10? Where do I do it? Thanks!

avatar image Cherno · Jan 12, 2015 at 11:09 PM 0
Share

Actually, since Random.Range with ints as parameters excludes the max value, it should be

  var indx: int = Random.Range(0,spawnPoints.Length);

(so, without the "-1" since random int will never reach the Length of the array anyway :) )

You should also be aware that the way the code currently works is that all 10 monsters will spawn at the same (random) spot. Is that really what you want, or do you want to have a random sport for each of the 10 monsters?

avatar image InvincibleCat · Jan 12, 2015 at 11:10 PM 0
Share

var nb$$anonymous$$onsters : int;

avatar image InvincibleCat · Jan 12, 2015 at 11:16 PM 0
Share

lol @Cherno you should have read my comment first (for the random part n_n)

avatar image The_Unity_Game_Developer · Jan 13, 2015 at 07:38 AM 0
Share

This is great thanks guys :) Also, @Cherno to answer your question, yes I want my monsters to be in different random spots. Only a few should be in the same spot. Thanks!

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

one prefab into random position of grid 0 Answers

Hi , whats a way i can instantiate a prefab list randomly but make sure at least one of the prefabs is a certain chosen one? 1 Answer

random prefab instantiation 2 Answers

How to generate a random number inside Unity? 7 Answers

triggering random animations with gui 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