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 19121989 · Mar 05, 2015 at 11:14 AM · instantiatelistfallingdropfill

Fill list with gameobject?

Hello, i'm newbie in unity. I want to fill an list like 4X5 or 5x4 with gems but not instantly smthing like 1 gem per second and when 4X5 is complete stop falling. this is my code. How can i do this?

 public class List : MonoBehaviour {
     
     public GameObject gem;
 
     public List<GameObject> gems = new List<GameObject>();
     
     // Use this for initialization
     void Start () {
         for (int y=0; y<5; y++) {
             for (int x=0; x<4; x++) {
 
             //    GameObject g =Instantiate(gem, new Vector3(x, y, 0), Quaternion.identity) as GameObject;
             //    gems.Add(g);
 
             }
         }
     }
     // Update is called once per frame
     void Update () {
 
         Vector3 fall = new Vector3 (1,2,0);
         gems.Add((GameObject)Instantiate(gem, fall, 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
2
Best Answer

Answer by Baste · Mar 05, 2015 at 01:56 PM

Two issues first: Please format code questions! Select the code you're pasting, and press the 0101 button.

Secondly, you shouldn't name your class "List", as there's already a built-in class named "List", and that'll end up causing confusion.

Now, to solve your problem, the easiest thing would be to use a Coroutine. In essence, they're methods that you can pause for a certain amount of time (or until an event is finished). You can read about them here.

Your code would be:

 public class GemThingy : MonoBehaviour {

     public GameObject gem;
     public List<GameObject> gems = new List<GameObject>();
     bool finishedSpawning;

     void Start() {
         StartCoroutine(SpawnCoroutine());
     }

     private IEnumerator SpawnCoroutine() {
         finishedSpawning = false;
         for (int y = 0; y < 5; y++) {
             for (int x = 0; x < 4; x++) {
                 GameObject g = Instantiate(gem, new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                 gems.Add(g);
                 //Wait for a second before continuing the for-loop
                 yield return new WaitForSeconds(1f);
             }
         }
         finishedSpawning = true;
     }

     void Update() {
         if (!finishedSpawning)
             return;

         //Make the gems fall
     }


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 19121989 · Mar 05, 2015 at 05:22 PM 0
Share

Tnx a lot man, this is it :D, now i play with this code and make it how i want :P.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Random instantiation endlessly? 1 Answer

Problem not remove all prefab from list aftre the instantiate 1 Answer

instantiate problem help? 1 Answer

About Instantiate 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