• 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
1
Question by SirBoboHobo · Mar 30, 2015 at 04:34 PM · c#androidinstantiaterandomplatformer

instantiates in the same place

I'm building a platformer, and I'm instantiating prefabs upwards, the problem is, that the prefabs can be instantiated on 1 another, causing multiple prefabs at the same spot, how can i generate them one AFTER another? there's my script, and its attached to a Quad attached to Main Camera using UnityEngine; using System.Collections;

 public class SpawnPlatfoms : MonoBehaviour {
     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
     // Use this for initialization
     void Start () {
         spawn();
     }
     
     // Update is called once per frame
     void spawn(){
         Instantiate (obj [Random.Range (0, obj.Length )], transform.position, Quaternion.identity);
         Invoke ("spawn", Random.Range (spawnMin, spawnMax));
     }
 }
 
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

Answer by lordlycastle · Mar 30, 2015 at 06:42 PM

Don’t call a function from itself, that’s ridiculous. Put it in update if you want it go every frame indefinitely, or use loops; that’s what they are for, looping. If you want them to instantiate at different positions then just change the second argument of Instantiate.

 Vector3 lastObjSpawnPos = new Vector3()
 void spawn(){
     int index = Random.Range(0, obj.Lenght -1);
     //If objects are different sizes, they need a collider to get their size.
     lastObjSpawnPos.y += obj[index].GetComponent<Collider>().bounds.extends.y;
     //otherwise you can just use cached size
     lastObjSpawnPos.y += 2
     Instantiate (obj [index], transform.position + lastObjSpawnPos, Quaternion.identity);
 }
 
Comment
Add comment · Show 1 · 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 SirBoboHobo · Mar 30, 2015 at 07:00 PM 0
Share

ummmm... I have a collider on each object, but my prefabs are like $$anonymous$$i levels that randomly generate, so I don't have a collider on the whole prefab but only on pieces from it. can i get the collider that its y position is the highest and from that collider +2? and also, it gives me an error UnityEngine.Bounds doesnt contain the definition for 'extends', maybe you meant extents?

avatar image
0

Answer by hbalint1 · Mar 30, 2015 at 06:48 PM

I don't understand your question perfectly. Do you want them to instantiate one after another and upwards? Then I think you should make Spawn as a Coroutine and wait after each spawn. Also you have to increase the y coordinate :

 public class SpawnPlatforms : MonoBehaviour
 {
     public int platformCount = 10;
     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
     public float waitBetweenSpawns = 1f;

     // Use this for initialization
     void Start()
     {
         StartCoroutine(Spawn());
     }
 
     IEnumerator Spawn()
     {
         for (int i = 0; i < platformCount; ++i)
         {
             Instantiate(obj[Random.Range(0, obj.Length)], new Vector3(0, i * 3, 0), Quaternion.identity);
             Invoke("spawn", Random.Range(spawnMin, spawnMax));
             yield return new WaitForSeconds(waitBetweenSpawns);
         }
     }
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

instantiate random prefabs based on player camera distance 1 Answer

instantiate problem 1 Answer

Random array issue C# 2 Answers

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

After picking up an object, spawn a new one in a random location 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