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 /
avatar image
0
Question by RlcZyro · Jul 09, 2019 at 06:12 PM · script.spawnspawning problems

unity chose random gameobject and spawn it.

So I haven't got a code for it yet but I have been struggling for a while trying to figure out how to spawn random gameobject out of about 10 at the location of a different gameobject of which the script is on. So every 5 seconds for example on of the gameobects will spawn on the gameobject with the script.

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

Answer by bpaynom · Jul 09, 2019 at 06:24 PM

So you have object A that has a script that spawns another object (1 of 10 randomly) at A's location. ? If that's the case:

 public Spawner : MonoBehaviour {
     [SerializeField] private GameObject[] objectsToSpawn;
 
     void Start()
     {
         Spawn();
     }
 
     public GameObject Spawn()
     {
         int index = objectsToSpawn.Length;
         if ( index > 0 )
         {
             index = Random.Range(0, index);
             return Instantiate ( objectsToSpawn[ index ] , transform.position, Quaternion.identity );
         }
         return null;
     }
 }

If you want random rotation, you can change Quaternion.identity for something like Quaternion.Lerp( rotationA , rotationB, Random.Range(0f,1f) ); , or you can set it as Quaternion.identity and then rotate the instantiated object Random degrees.

Comment
Add comment · Show 4 · 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 I_Am_Err00r · Jul 09, 2019 at 06:44 PM 0
Share

I like this! The only thing I would add is you would need to attach this script to the transform that you want to spawn at, or you could tweak a bit with this, just create an empty game object and then attach drag it into the spawnPosition slot made in this script:

 public Spawner : $$anonymous$$onoBehaviour 
 {
      [SerializeField] private GameObject[] objectsToSpawn;
      public Transform spawnPosition;
 
      void Start()
      {
          Spawn();
      }
  
      public GameObject Spawn()
      {
          int index = objectsToSpawn.Length;
          if ( index > 0 )
          {
              index = Random.Range(0, index);
              return Instantiate ( objectsToSpawn[ index ] , spawnPosition.position, Quaternion.identity );
          }
          return null;
      }
  }
avatar image RlcZyro · Jul 09, 2019 at 06:44 PM 0
Share

@ConcanoPayno Ok so I now have an object spawning every one second. How would I give it a random range of rotations eg. transform.rotation(0, 0, Random.Range(22, -22),

avatar image I_Am_Err00r RlcZyro · Jul 09, 2019 at 07:26 PM 0
Share

Now it is starting to get a bit more convoluted and requires more of an understanding of the fundamentals of coding in general; also I'm at work and don't have an IDE that can compile this code, but it should more or less give you what you are asking for:

 public Spawner : $$anonymous$$onoBehaviour 
  {
       [SerializeField] private GameObject[] objectsToSpawn;
       public Transform spawnPosition;
     public float timeBetweenSpawns;
      bool isSpawning;
     
     void Update()
     {
             if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)
             {
              isSpawning = true;
             }
         float countDown = timeBetweenSpawns
         if(isSpawning == true)
            {
             int index = objectsToSpawn.Length;
                    if ( index > 0 )
                    {
                        index = Random.Range(0, index);
               countDown -= time.deltaTime;
               if (countDown <= 0)
               {
                            Instantiate ( objectsToSpawn[ index ] , spawnPosition.position, Quaternion.identity );
                 countDown = timeBetweenSpawns
             }
                  if(index < 0)
                  {
                         isSpawning = false;
                   }
         }
     }

There are other ways with coroutines and having a much cleaner update function that would check when to start spawning and stop spawning, but writing the functions in a coroutine, update, or fixedUpdate are the only ways to control time.

avatar image bpaynom I_Am_Err00r · Jul 09, 2019 at 07:30 PM 0
Share

He talks about random rotation. He said he already has it working every second. I updated my answer introducing the rotation thing.

avatar image
0

Answer by RlcZyro · Jul 09, 2019 at 08:08 PM

Thanks man your scripts really helped and with a bit of tweaking I got it working :)

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

125 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

Related Questions

I need To spawn game objects randomly, but I'm Getting an error in My script, Please help! 1 Answer

How to prevent multiple spawns from a single spawn point? 1 Answer

Object Pickup and Respawn unity 0 Answers

spawn road 0 Answers

How do I make enemies not spawn in the same position? 2 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