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 SirBoboHobo · Apr 02, 2015 at 03:16 PM · c#instantiatepositionrandomprefabs

instantiate prefabs y+1 from last instintiated prefab

I've got 15 prefabs. and i would like them to generate randomly above each other, lets say every 3 seconds, I've got this script, but it generates one on top another, i need to generate a prefab above the last prefab, like .y+1

 public GameObject[] obj;


 // Use this for initialization
 void Update () {
 
 }


 void Switch(){
     Instantiate (obj [Random.Range(0, obj.Length)], transform.position, Quaternion.identity);

 }

}

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
0

Answer by POiD · Apr 02, 2015 at 03:23 PM

 //drag your prefabs into this public array from the inspector
 public GameObject[] myPrefabs;
 
 IEnumerator generatePrefabs()
 {
    int currentY = 0;
    while (currentY < myPrefabs.Length)
    {
       Instantiate(myPrefabs[currentY],new Vector3(<your X value>,<your Y Value> + currentY,<your Z Value>),Quaternion.identity);
        currentY++;
        yield return new WaitForSeconds(3f);
    }
 }

In some function .. that triggers these to start generating, add the following line:

StartCoroutine("generatePrefabs");

If you want the prefabs to be generated in a random order, then you'll have to add some more logic to 'tick them off' as selected. Thus an array the same size as your myPrefabs array, that just holds a 'used' or 'not used' value. Generate a random number, if it's not used, instantiate it and mark as used. If used, generate another number until you have an unused prefab spot.

Comment
Add comment · Show 7 · 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 · Apr 02, 2015 at 08:56 PM 0
Share

the while loop will stop the instantiate after the number of my prefabs, Its a platformer, supposed to go till the player dies and not run out of levels, also cant use waitforseconds since this might cause gaps between prefabs making the game not playable because the player cant reach

avatar image POiD · Apr 03, 2015 at 12:48 AM 0
Share

Understood. You can easily change the while loop to ins$$anonymous$$d be "while (true)", but that is likely not what you want anyhow.

Are you moving the Camera or are you moving the scenery? That really deter$$anonymous$$es the algorithm you use for instantiating (and cleaning up) your prefabs.

If moving the scenery (i.e. scrolling it from just above the camera window to below), then you can instantiate at the same point just out of scene and destroy when below the camera window. Thus your Y coord for instantiating stays the same. You just have a timer to decide when to spawn the next one. Also means you can use an Object Pool to re-use the same objects.

If moving the camera, then you'd need to get the Y position of your main Camera. Then instantiate the next prefab at (Camera Y position [transform.position.y] + height of camera/2), to create the prefab at the top of the screen or just outside of it. Again a timer can be used to decide how much of a gap before you create the next one.

All depends on the design of your game.

avatar image SirBoboHobo · Apr 03, 2015 at 08:21 AM 0
Share

$$anonymous$$y camera has a smooth follow attached to my character which moves jumps up on platforms, I have a quad with trigger below the camera to destroy objects when going up. i can't pool object because my prefabs are like in jetpack, where the prefabs are built from a few platforms together at different positions, like "$$anonymous$$i levels", and they all different sizes. basically what i need is to find the y of the last spawned prefab and next spawn y+2 from that object, like bounds.extants.y but im not sure what i can put as a component to measure the y, a trigger falls off the table because it screws up the physics in my game (OnTriggerEnter on my player)

avatar image POiD · Apr 03, 2015 at 02:00 PM 0
Share

Why not just store the Y of the last prefab you created? Store it in the class you are creating the prefabs in, or as a static variable if you really need to?

I'm afraid I can't visualize how you've designed your game, so my only other potential suggestion for your trigger idea, is to add an empty child object to your player and have a collider on that object that you set as a trigger. Wouldn't affect the player physics as that'll be on the collider / rigidbody on the parent (the player).

avatar image SirBoboHobo · Apr 03, 2015 at 08:25 PM 0
Share

what point will it choose as the y? bottom top or middle, each prefab is different height, can i ask for the lowest y and highest y, then subtract high to low, and then add the number ive got +2 to instantiate next one?

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

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

Instatiating a prefab in a random position 0 Answers

Random array issue C# 2 Answers

What the heck is wrong with this script?! 1 Answer

Creating Prefabs at runtime? 1 Answer

moving random objects to random positions 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