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 FatalNightmare · Mar 17, 2015 at 11:54 PM · randomspawning

Random spawning, but one definite object

Hi, I am new to coding so I don't know if this is a basic question or not. I would like a code that picks randomly 1 object from a list of 3 and spawns that object at each of my 5 spawn points. Here is the thing I don't know how to do; I want it to always spawn 1 specific object everytime at 1 random spawn point of the 5. How would I go about doing this?

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 DiGiaCom-Tech · May 27, 2016 at 10:52 PM 0
Share

@FatalNightmare ... how about marking the correct answer if this helped you with your coding?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiGiaCom-Tech · Mar 18, 2015 at 01:59 AM

Basically you'll need an array of GameObjects to be spawned and an array of Vector3 locations where these objects could be spawned to. Once these are populated you then randomly select an object and and then randomly select a location. Once this is done you instantiate the object at the selected location. Something like this perhaps...

 public GameObject[] MyObjects;
 public Vector3[] MyLocations;

You can then set/populate these in the inspector. Next in your 'Update' method you'll need some sort of timer to trigger the spawning -or- an method that can be called to trigger the spawning. This method will call a function to generate the two random numbers.

 int ObjectID = Random.Range(0, 2); // Returns integer from 0 to 2
 int LocationID = Random.Range(0, 5); // Returns integer from 0 to 4
 

Finally you can call the instantiate method to make it happen...

 GameObject SpawnedObject = (GameObject)Instantiate(MyObjects[ObjectID], MyLocations[LocationID ], Quaternion.identity);

Those are the basics ... hope this helps ;)


UPDATE

Ok ... looks like I made one typo on the randomization code (should have been 3 not 2) but here is a script for you to try ;)

It has both a timed spawn and a triggered spawn (e.g. a GUI button). Note that the Instantiate code is returning the spawned object into a GameObject variable called SpawnedObject so you can alter it after it's spawned (e.g. re-name/re-tag, change scale/rotation/materials, etc.).

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour
 {
     public GameObject[] MyObjects;
     public Transform[] MyLocations;
     public float MyTimer = 5f;
 
     private float CurTime;
 
     // Use this for initialization
     void Start () {
         // Initialize the timer value
         CurTime = MyTimer;
     }
     
     // Update is called once per frame
     void Update () {
 
         // Decrement timer value
         CurTime -= Time.deltaTime;
         // If time has run out
         if (CurTime <= 0f)
         {
             // Call the spawner
             SpawnTrigger(); 
             // Reset the timer value
             CurTime = MyTimer;
         }
     }
 
     void OnGUI()
     {
         //If they click the button
         if (GUI.Button(new Rect(Screen.width / 2f - 50f, Screen.height * 0.1f, 100f, 20f), "Spawn Now"))
         {
             // Call the spawner
             SpawnTrigger();
             // Reset the timer value
             CurTime = MyTimer;
         }
     }
 
     void SpawnTrigger()
     {
         int ObjectID = Random.Range(0, 3); // Returns integer from 0 to 2
         int LocationID = Random.Range(0, 5); // Returns integer from 0 to 4
 
         GameObject SpawnedObject = (GameObject)Instantiate(MyObjects[ObjectID], MyLocations[LocationID].position + Vector3.up * 25f, Quaternion.identity);
     }
 }
   

Enjoy ;)

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

21 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

Related Questions

Instantiate random object from Array 2 Answers

I want to swap randomly 5 blocks (block has 5 diffrent color) and spawn in vertical line 1 Answer

Weighted Random Spawning 1 Answer

Enemies spawn on top of each other(C#)(Unity) 1 Answer

How do I make a game object spawn several times in semi-random locations? 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