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 Mahr_Mudasser · Oct 10, 2017 at 01:50 PM · spawnspawningspawning problemsspawnpoints

Is it possible to instantiate a prefab in 2d game from particular locations without using empty game object?

I am working on a 2d game i i want to spawn different prefabs randomly from random positions. i am using empty game objects at different positions and using random function to generate enemies from different positions.. what i need is i want to spawn enemies without using empty game objects from different locations. how can i use those locations in array and in my random method ? hope so you got what i really want to know.

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
Best Answer

Answer by Yuvii · Oct 10, 2017 at 02:36 PM

Hey,

Not sure if i get it, but i'll try.

In the unity doc you can see that you can instantiate like that

 Instantiate(Object original, Vector3 position, Quaternion rotation);
 

It doesn't realy need an empty game object, but a vector 3. So you can try something like

 Instantiate(*your object*, new Vector3 ( Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ, maxZ)), *your quaternion*);
 

Well in a 2D game you only need 2 axes, but you get the idea :-)

Comment
Add comment · Show 6 · 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 Mahr_Mudasser · Oct 10, 2017 at 05:20 PM 0
Share

yes i am clear enough . one thing that i am not getting is how to put my positions in n array ? for example i have 8 positions from where i want to instantiate objects randomly.. so how can i put positions in array and generate randon positions here new Vector3 ( Random.Range($$anonymous$$X, maxX), Random.Range($$anonymous$$Y, maxY), Random.Range($$anonymous$$Z, maxZ)) ???????

avatar image NoseKills Mahr_Mudasser · Oct 10, 2017 at 06:58 PM 0
Share

Just declare a public Vector2[] myPositions; variable in your monobehaviour and you can insert as many Vector2s in it as you like in the inspector.

then choose a random one from them with Vector2 selectedPosition = myPositions[Random.Range(0, myPositions.Length)]

avatar image Mahr_Mudasser · Oct 10, 2017 at 05:25 PM 0
Share

in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a randon location and use that locat

avatar image Mahr_Mudasser · Oct 10, 2017 at 05:27 PM 0
Share

@Yuvii in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a randon location and use that locat

avatar image Mahr_Mudasser · Oct 10, 2017 at 05:28 PM 0
Share

@Yuvii in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a random location and use that location to instantiate prefab.

avatar image Yuvii Mahr_Mudasser · Oct 11, 2017 at 08:34 AM 0
Share

Hey sorry i just saw your comment.

Well to do so, what i'd do myself is keep the empty gameobjects system and then add a random vector3 as an offset to it.

So something like that :

 public Transform[] differentSpawners;
 
 Vector2 offset = new Vector2(Random.Range($$anonymous$$X, maxX), Random.Range($$anonymous$$Y, maxY));

 Instantiate(*your object*, differentSpawners[Random.Range(0, differentSpawners.Lenght)].position + offset, *your quaternion*);


The advantage of keeping the Empty GO system is that you can easily move them in the inspector.

Hope that helped :)

avatar image
0

Answer by marcrem · Oct 10, 2017 at 03:26 PM

Yuvii's answer is pretty much my solution. However if you wanted to specify locations to randomly choose from, then just put some Vector3s in an array!

Comment
Add comment · Show 4 · 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 Mahr_Mudasser · Oct 10, 2017 at 05:21 PM 0
Share

yes i am clear enough . one thing that i am not getting is how to put my positions in n array ? for example i have 8 positions from where i want to instantiate objects randomly.. so how can i put positions in array and generate randon positions here new Vector3 ( Random.Range($$anonymous$$X, maxX), Random.Range($$anonymous$$Y, maxY), Random.Range($$anonymous$$Z, maxZ)) ???????

avatar image Mahr_Mudasser · Oct 10, 2017 at 05:25 PM 0
Share

in clear words i want to ask that i have 8 locations .. how i can put those locations in an array and generate a randon location and use that locat

avatar image marcrem Mahr_Mudasser · Oct 10, 2017 at 05:38 PM 0
Share

I'll go with how I'd do it, although I might not be using the best way.

I'd do a List.

so at the top of your code you declare a list : List positionsList;

then in your start function: positionsList = new List();

Then, after that, you can add as many vectors as you'd like to the list, by doing: positionsList.Add(new Vector3(x,y,z));

So now, cool! You've added vectors to the list. Now how do you randomly pick one?

at the top of your code, delare an int: int pickedPosition;

then, whereever you want to randomly choose one of the positions in your vector list, you would do this:

pickedPosition = Random.Range(0, (positionsList.Count - 1)); // choose between 0 (the first vector in your list) and the last one, meaning the total count of your list $$anonymous$$us one because 0 counts for one. Instantiate(yourObject, positionsList[pickedPosition], Quaternion rotation); //Here it's going to spawn your object at the previously randomly picked item on your list.

Hope that helps!

avatar image marcrem Mahr_Mudasser · Oct 10, 2017 at 05:42 PM 0
Share

in the instantiate line, you might wanna change "Quaternion rotation" to say.. Quaternion.identity

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

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

Related Questions

How to prevent multiple spawns from a single spawn point? 1 Answer

How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers

[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers

Spawning help 1 Answer

Being spawned inaccurately on level load. 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