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 localhost · Sep 18, 2012 at 12:17 PM · objectspawncube

Spawn object in cube

Hello,

I Was wondering if anyone could help me with this; So i have 8 cubes, which would possibly use a trigger, anyway. I Have an object, and i want to spawn the object inside ONE OF THE CUBES, so it's different every time, it could be cube 1, or 2 or 3 or 4 or 5, and so on. So it chooses one cube and spawns the object there.

Could anyone possibly help me out with how i would do this?

Thanks

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 AlucardJay · Sep 18, 2012 at 12:20 PM 1
Share

Store your 8 cubes in a list/array. When it comes to spawn time, choode a random number, find that number cube in the list, spawn object at chosen cube.

Search 'unity spawn points' , this is an example of what I am describing : http://answers.unity3d.com/questions/13657/mulitple-spawn-points-with-random-seed.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mwinteringham · Sep 18, 2012 at 12:45 PM

I am not going to write the code but I see the logic as this:

  1. Randomly generate a number between 1 and 8. This can be done by using the Random class: http://docs.unity3d.com/Documentation/ScriptReference/Random.html

  2. Once you have your number, you will need to find your objects. I would set all the objects with same tag and use GameObject.FindGameObjectsWithTag to return an array of all the Cubes you want to spawn into: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

  3. Once you have your array of GameObjects, use the randomised number from step 1 to select a specific GameObject and use that GameObject's position as a the location for your initial object Object to spawn

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
avatar image
0

Answer by inco · Sep 18, 2012 at 12:45 PM

 YourCubeObjectType[] cubes = FindObjectsOfType(typeof(YourCubeObjectType)) as YourCubeObjectType[];
 Instantiate(YourSpawnObjectType, cubes[Random.Range(0, cubes.Length)].transform.position, Quaternion.identity)

Should do it (it's almost the same in JS if that's your preference). Of course, you can use Vector3.zero position for instantiation, and then you can add your spawned object as a child of one of the cubes if you wish for it to move with the parent cube like:

 YourCubeObjectType[] cubes = FindObjectsOfType(typeof(YourCubeObjectType)) as YourCubeObjectType[];
 YourSpawnObjectType spawned = Instantiate(YourSpawnObjectType, Vector3.zero, Quaternion.identity)
 spawned.transform.parent = cubes[Random.Range(0, cubes.Length)].transform;
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

14 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

Related Questions

Enemy object spawning before move action is complete :/ 1 Answer

Object spawned during collision: Object's Y axis to be colliding object's polygon's direction 1 Answer

Making a simple grid 2 Answers

How to make an object float in space 4 Answers

obect will not appear in scene 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