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 lemiwinks · Feb 05, 2014 at 07:28 PM · prefabrandomspawn

spawning random objects at 2 defined points

hey all, so ive got two empty's set up , one on the top and one on the bottom of the screen. I have 4 different shaped objects that are prefabs, what i wish to do is take it in turns every second or so and spawn 1 of the 4 objects either at the top empty of the bottom one, taking it in turns with each spawn location. i have no idea where to begin lol. i have got an outline of what needs to happen

 -1 second PASSED
 -random pick object out of 4 prefabs
 -spawn object at top -CHANGE ACTIVE SPAWN TO BOTTOM-
 -1 second PASSED
 -random pick object out of 4 prefabs
 -spawn object at bottom -CHANGE ACTIVE SPAWN TO TOP-
 -REPEAT

thankyou

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by NickP_2 · Feb 05, 2014 at 08:20 PM

If you put this in a c# script, define the top and bottom position in the scripts inspector, and the prefabs in the array in the inspector, this will keep spawning a random object randomly, switching from a top position to bottom position

     public Transform[] objects;
     
     public Vector3 topPosition, downPosition;
     
     private bool spawning = false, isTop = true;
     
     void Update()
     {
         //if not spawning, start a new spawn
         if(!spawning)
             StartCoroutine(Spawn());
     }
     
     IEnumerator Spawn()
     {
         spawning = true;
         //select random object out of array
         Transform randomObj = objects[Random.Range(0, objects.Length)];
 
         Transform clone;
         switch(isTop)
         {
             case true:
                 clone = Instantiate(randomObj, topPosition, Quaternion.identity) as Transform;
                 isTop = false;
                 break;
             case false:
                 clone = Instantiate(randomObj, downPosition, Quaternion.identity) as Transform;
                 isTop = true;
                 break;
         }
         yield return new WaitForSeconds(1);
         spawning = false;
     }
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 lemiwinks · Feb 05, 2014 at 08:53 PM 0
Share

Thankyou! that works great however how could i make it so the spawn points are set to an empty's position? the camera is always moving to the right so having set x,y and z positions doesnt work. sorry to be a bother. also would it be difficult to make 2 sets of arrays? if top spawn is used it picks and object from one of the arrays, wheer bottom spawn picks a random object from the other set.

again thankyou

avatar image lemiwinks · Feb 05, 2014 at 09:38 PM 1
Share

never$$anonymous$$d about the two arrays, i managed to make a model that can be used both ways :)

avatar image NickP_2 · Feb 05, 2014 at 09:48 PM 1
Share

Can you define "empty's position"? Do you mean a Vector3.zero? which is 0 for x, y, z?

avatar image lemiwinks · Feb 05, 2014 at 10:00 PM 0
Share

yup, just done that and works great :) thanks again

avatar image NickP_2 · Feb 05, 2014 at 10:05 PM 0
Share

Np, lemiwinks, wait, the frog lemiwinks? :P

Show more comments

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

19 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

Related Questions

Spawn random prefabs at multiple locations 1 Answer

Problem with random spawning and coroutine 1 Answer

How can I align instantiated prefabs randomly on runtime #C 0 Answers

How to make an object/prefab choose between multiple given positions to spawn into? 1 Answer

How do I spawn a random prefab? 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