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
2
Question by lixpoxx · Sep 11, 2013 at 04:50 PM · prefabrandomspawnspawnercoordinate

Random Spawn, Random Prefab

Hey, I have made 9 spawn-points in my game, and i want to randomly spawn 1 of 4 prefabs! I don't really know how to script it, that the code isn't around 100 lines. Can anyone help me?

THX lixpoxx

P.S.:It would also be very nice if you can tell me how to make it, that the spawner sends a new Prefab if the old is ca. 1 Coordinate away!

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

Answer by YoungDeveloper · Sep 11, 2013 at 05:21 PM

Create a public array of 9 Transforms (or whatever depending on your teleport), and public array of 4 prefebs (GameObject). Generate two random number, like this:

 int teleport = Random.Range(0,8);
 int prefeb = Random.Range(0,3);

Then spawn it using "Instantiate": http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

 Instantiate(gameobject_array[prefeb], teleport.position, teleport.rotation );

Save it, and add 9 transforms and 4 prefebs in inspector.

Comment
Add comment · Show 3 · 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 lixpoxx · Sep 11, 2013 at 06:10 PM 0
Share

I don't know much about arrays, can you show me how you would do it?

avatar image YoungDeveloper · Sep 11, 2013 at 09:42 PM 1
Share
 public Transform[] teleport;
 public GameObject[] prefeb;
 
 void Start(){ //this will spawn only one prefeb, if you want call it many time, create  a new function and call it or create for loop
 int tele_num = Random.Range(0,8);
 int prefeb_num = Random.Range(0,3);
 
 Instantiate(prefeb[prefeb_num], teleport[tele_num].position, teleport[tele_num].rotation );
 
 }

 //And remember to asign all teleport transforms and prefebs in inspector
avatar image yummybrainz · Mar 15, 2014 at 03:53 AM 0
Share

This is awesome! Thanks for sharing.

avatar image
0

Answer by nithu · Nov 21, 2015 at 11:58 AM

using UnityEngine; using System.Collections;

public class spawner : MonoBehaviour {

 // Use this for initialization
 int m_CurrentSpawnCount=0,SpawnCount = 10;
 public GameObject Enemy;
 void Start () {
     StartCoroutine("delayCall");
 }
 
 // Update is called once per frame
 IEnumerator  delayCall () {
     //if(m_CurrentSpawnCount <  SpawnCount)
     {

         for(int i = 0 ; i < 10 ; i++) // try 50 times. Brute force approach, randomly try to spawn and make sure its in the Spawnable zone. 
         {
             Vector3 position = transform.position + Random.insideUnitSphere * 100;
             position.y = this.transform.position.y;
             RaycastHit hitInfo;
             if(Physics.Raycast(position + new Vector3(0,1,0), Vector3.down,out hitInfo,10)&& hitInfo.collider.tag == "Spawnable")
             {

                 Instantiate(Enemy,position,Quaternion.identity);
                 yield return new WaitForSeconds(1);
                 StartCoroutine("delayCall");
                 break;
             }
         }
     }
 }

}

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

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

18 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

Related Questions

How To Random Range a Float? 2 Answers

How to randomly spawn object without spawning the previous object in a row? 0 Answers

Random Prefab Spawner C# 2 Answers

Prefab script values do not update 2 Answers

spawning random objects at 2 defined points 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