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 /
avatar image
0
Question by PaincideStudio · Feb 13, 2018 at 08:04 AM · randomspawnrandom.range

Object spawn at spawn points


Hello!
I want to make a spawn points on or in the furnitures(desk,chair,closet,etc) and make the objects spawn randomly.
There will be like 9 spawn points.
The objects don't need a random Z( if Z is random, then the objects are gonna float from the furnitures).
Help me plz!

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 Hellium · Feb 13, 2018 at 09:16 AM

  1. Create a new Empty gameobject,

  2. Create a new script called Spawner and copy-paste the following code

  3. In your scene, create new empties and drag & drop them in the spawnPoints array in the inspector of the Spawner component

  4. Get a reference to the Spawner script (using public attribute to your own class, or by using FindObjectOfType<Spawner>() and call the PlaceTransform function


 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour
 {
     [SerializeField]
     private System.Collections.Generic.List<Transform> spawnPoints;
 
     private System.Collections.Generic.List<Transform> availableSpawnPoints;
 
     private void Awake()
     {
         availableSpawnPoints = new System.Collections.Generic.List<Transform>(spawnPoints);
     }
 
     public void PlaceTransform( Transform transformToPlace )
     {
         if (availableSpawnPoints.Count > 0)
         {
             int index = Random.Range(0, availableSpawnPoints.Count);
             transformToPlace.position = availableSpawnPoints[index].position;
             availableSpawnPoints.RemoveAt(index);
         }
         else
         {
             Debug.LogError("No spawn point available");
         }
     }
 }


Comment
Add comment · Show 9 · 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 PaincideStudio · Feb 13, 2018 at 03:23 PM 0
Share


Thanks for helping. But I can't understand after 3.
I made a Spawner script.
And I put the empty gameobjects on the place where I'm trying to use as spawn points.
But I got stuck after that.
Can you explain with a photo??

avatar image Hellium PaincideStudio · Feb 13, 2018 at 05:26 PM 0
Share

Supposing you have a script responsible for managing your items you want to place randomly:

 using UnityEngine;
  using System.Collections;
  
  public class $$anonymous$$yItems$$anonymous$$anager : $$anonymous$$onoBehaviour
  {
      // Drag & drop the gamobjects to place randomly
      [SerializeField]
      private Transform[] objectsToPlace ;

      // Drag & drop the gamobject with the `Spawner` script
      [SerializeField]
      private Spawner spawner ;
       
      private void Start()
      {
          for( int i = 0 ; i < objectsToPlace.Length ; ++i ) spawner.PlaceTransform( objectsToPlace[i] ) ;
      }
  }
avatar image PaincideStudio Hellium · Feb 14, 2018 at 09:16 AM 0
Share

alt text

spawner.png (5.8 kB)
Show more comments
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

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

Related Questions

how to Spawn prefab infront off player and destroy it when hr passed it 0 Answers

Random Object Spawn 0 Answers

Random.value for each GameObject. 1 Answer

How to spawn a prefab every 5 second randomly between a set range? 0 Answers

Need help with specifics in Random.Range 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