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 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
Add comment · 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
5
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
Add comment · 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
2

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
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
avatar image
0

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
Add comment · 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.

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

Spawn object from Random Vector3 in array 0 Answers

Drop item from airplane 1 Answer

Random death instantiate? 1 Answer

Instantiate Prefab at random 1 Answer

How would i use a 2D array for level generation 0 Answers


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