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 /
  • Help Room /
This question was closed Mar 28, 2019 at 03:55 AM by DeltaTwoZero for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DeltaTwoZero · Mar 17, 2017 at 03:18 PM · gameobjecttransform3darraysspawnpoints

[SOLVED]How do I spawn GameObject[] in Transform[]?

I have 2 arrays: GameObject[] enemy; Transform[] spawns;

How do I randomly spawn enemies at random spawn point?

Edit: I was looking for this: GameObject temp = (GameObject)Instantiate(enemy[(Random.Range(0, 2))], spawns[(Random.Range(0, 4))].transform.position, Quaternion.identity);

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

  • Sort: 
avatar image
1
Best Answer

Answer by Commoble · Mar 17, 2017 at 03:28 PM

Instantiate creates a copy of a prefab or an existing gameobject. The UnityEngine.Random static class is a simple way to get random numbers.

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 DeltaTwoZero · Mar 17, 2017 at 07:45 PM 0
Share

Something like this? void SpawnEnemy() { GameObject temp = (GameObject)Instantiate(enemy[Random.Range(0,2)], spawn[Random.Range(0, 4)], Quaternion.identity); }

avatar image
0

Answer by Ahsenhein · Mar 17, 2017 at 07:30 PM

I'm a beginner too but I think if you use a navmesh and prebake everything then you just need to create a loop and set a random location(with Random.Range()) to the agent and then ask if It's a valid path by using NavMeshAgent.HasPath. Then If true store this coordinate in your Transform[] spawns;

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 DeltaTwoZero · Mar 17, 2017 at 07:46 PM 0
Share

I'm currently new to Nav$$anonymous$$eshAgent. Can you give me an example of your code if you have one?

avatar image Ahsenhein DeltaTwoZero · Mar 17, 2017 at 08:37 PM 0
Share

Something like this

         float posX = enemy.transform.position.x + Random.Range(-5, 5);
         float posZ = enemy.transform.position.z + Random.Range(-5, 5);
         Vector3 randomPos = new Vector3(posX, enemy.transform.position.y, posZ);

         Nav$$anonymous$$eshPath path = new Nav$$anonymous$$eshPath();
         if (enemy.nav$$anonymous$$eshAgent.CalculatePath(randomPos, path))
        {
                     if (enemy.nav$$anonymous$$eshAgent.hasPath)
        {

and so on... but this is complicated for us. I think you should just create empty game objects as spawnpoints and spread them all over the place and then pick one randomly. On start you can feed your array with their locations. and use a loop to get one random index.

Follow this Question

Answers Answers and Comments

122 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

Related Questions

Return Object to Original Position after it's Disabled 1 Answer

How to get the position of a gameObject based on its index in an array? 0 Answers

How To Make An Object Appear In Front Of Player Without Cloning? 1 Answer

UnityException: Transform child out of bounds (C# Unity) 0 Answers

Is it possible to summon specific GameObjects (i.e. from Assets) on a specific position in a jagged array so that I can later Destroy said object? 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