• 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
Question by satnav_8 · Nov 26, 2012 at 06:27 PM · instantiatevector3fpsrandomspawn

Spawning objects at random Vector3

I've got some ammo pickups around my game map, working perfectly. Now all I need to do is get them spawning at random points at runtime. I thought to save problems with height, I'd spawn them up in the air, and let them fall and land on the terrain surface. The ammo boxes spawn fine, but all at 0, 0, 0. And I can't work out why.

 using UnityEngine;
 using System.Collections;
 
 public class ammoSpawn : MonoBehaviour {
 
     public Transform ammoDrop;
     public int numToSpawn;
     public Vector3 position;
     
     void Awake()
     {
         Vector3 position = new Vector3(Random.Range(100.0F, 1000.0F), 70, Random.Range(100.0F, 1000.0F));
     }
     
     void Start() 
     {
         int spawned = 0;
         
         while (spawned < numToSpawn)
         {
             spawned++;
             Instantiate(ammoDrop, position, Quaternion.identity);
         }
     }
     
 }

I know in this code, all will be spawned at one point, but why 0, 0, 0? Surely thats not right!

Comment

People who like this

0 Show 1
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 satnav_8 · Nov 26, 2012 at 06:35 PM 0
Share

Oh, I got it. In the Awake() function, I didn't need to declare it was a Vector3 again. So now, how would I go about spawning them at individual random points?

3 Replies

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

Answer by PaulUsul · Nov 26, 2012 at 06:39 PM

The trick is that the Random.Range generates random numbers every time you call it.

     void Start() 
     {
        int spawned = 0;
 
        while (spawned < numToSpawn)
        {
           position = new Vector3(Random.Range(100.0F, 1000.0F), 70, Random.Range(100.0F, 1000.0F));
           Instantiate(ammoDrop, position, Quaternion.identity);
           spawned++;
        }
     }

This way a new position is generated every time before you spawn the object.

Comment
satnav_8
cregox
kristaps_z
StartStart
abandonrules

People who like this

5 Show 1 · 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 satnav_8 · Nov 27, 2012 at 05:04 PM 0
Share

Thanks, thats perfect!

avatar image

Answer by flaviusxvii · Nov 26, 2012 at 07:03 PM

Your variable "position" in Awake() is shadowing the data member "position" of the class.

Comment
Loius
SarahU

People who like this

2 Show 0 · 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

Answer by Meltdown · Nov 26, 2012 at 06:40 PM

I'd suggest tagging all your spawnpoints with a tag called 'spawnpoint', then use GameObject.FindGameObjectsWithTag() to get all the spawn points in your scene.

Then use Random.Range() to choose one of those random spawn points in your scene to instantiate your game object at.

Comment

People who like this

0 Show 2 · 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 vverma9 · Feb 21, 2017 at 08:08 AM 0
Share

and how would you ensure that the same spawn point is not selected again?

avatar image SarahU vverma9 · Feb 21, 2017 at 09:13 AM 0
Share

You can store Id of last selected spawn point and generate new ids until you generate a different one.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Instantiate multiple objects and have them raycast 1 Answer

Gameobject spawns at a weird position 1 Answer

Instantiate Random 3D Objects/platforms that dont overlap 1 Answer

avoid overlap problem when randomly instantiating 0 Answers

Spawning Objects Using An Array. 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