• 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 unity_Clehe_ePfKrh-g · Oct 11, 2020 at 02:22 PM · 3dspawnspawningspawning problemsspawnpoints

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

Hello all, I'm making a game which requires the player to find a door which has spawned randomly in the map with a simple portal animation, which when entered, will project a screen with the text "YOU WIN". I did figure out those screens, movement, and everything, except spawning the door on random 4-5 places, I am a literal beginner who started unity almost a week ago, any code which I could understand (C#) would be appreciated, Thanks :)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Simonotos · Oct 11, 2020 at 03:54 PM

 Random r = new Random()
 int door_count = r.Range(1,5) 
 
 switch(door_count){
 
        case(1) : 
                  door.transform.position = first_point;
        break;
 
        case(2) : 
                  door.transform.position = second_point;
        break;
 
        case(3) : 
                  door.transform.position = third_point;
        break;
 
        case(4) : 
                  door.transform.position = fourth_point;
        break;
 }


The points refers to public Vector2/3 where u put the position u desire to spawn door if u can't manage to use Random class u can try with System.Random Hope it helps u!

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 unity_Clehe_ePfKrh-g · Oct 12, 2020 at 03:07 AM 0
Share

Thanks, Will Try :)

avatar image
1

Answer by tekinomer241 · Oct 19, 2020 at 01:30 PM

You may choose some specified(by you) points to be spawn points and then pick one of them randomly. You can define it by:

[SerializeField] private Transform[] spawnPoints;

And of course to instantiate the door you need to have it:

[SerializeField] private GameObject door;

To spawn the door you can use a code like this one:

Instantiate(door, _spawnPoints[Random.Range(0, _spawnPoints.Length)]);

Don't forget to put the right objects to the script component in the inspector.

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 unity_Clehe_ePfKrh-g · Dec 02, 2020 at 01:23 PM 0
Share

Thank You, this worked!!!!!

avatar image
-1

Answer by BRX99_GS · Oct 19, 2020 at 01:14 PM

I don't understand you very well but maybe this will help.

     // Create and position the object.
     GameObject door = GameObject.CreatePrimitive(PrimitiveType.door);
     door.transform.position = new Vector3(0.0f, -1.0f, 0.0f);
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 Hellium · Oct 19, 2020 at 01:17 PM 0
Share
  • door is not part of the PrimitiveType enum

  • You are setting an absolute position, not a random position as asked by the original poster

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

187 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 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 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 do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers

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

Spawning help 1 Answer

How do I make enemies not spawn in the same position? 2 Answers

How to spawn enemies at different locations and avoid overlapping each other 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges