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 davidflynn2 · Jul 10, 2013 at 07:15 PM · c#spawning

Spawning A Grid Of Cubes

I am using the following script to spawn a grid of cubes in my scene the problem is while its spawining if you move the screen it throws the cubes all over the place. Also if you change the worldWidth or Height for the first time you press play it places the first cube in the wrong place.

 using UnityEngine;
 using System.Collections;
 
 public class WorldSpawn : MonoBehaviour {
 
 
 public GameObject block1; 
 int worldWidth  = 10;
 int worldHeight  = 10;
 float spawnSpeed = 0;
  
 void  Start ()
     {
 StartCoroutine(CreateWorld());
 }
  
 IEnumerator CreateWorld (){
  
     for(int x =0; x<worldWidth; x+=1) {
       yield return new WaitForSeconds(spawnSpeed);
      for(int z =0; z<worldHeight; z+=1) {
       yield return new WaitForSeconds(spawnSpeed);
  
       GameObject block = Instantiate(block1,block1.transform.position, block1.transform.rotation)as GameObject;
  
       block1.transform.position = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + z);
  
       }
  
       }
  
       }
 }
 
 
Comment
Add comment · Show 9
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 DavidDebnar · Jul 10, 2013 at 07:20 PM 1
Share

Let me guess, this script is attached to the camera, isn't it?

avatar image davidflynn2 · Jul 10, 2013 at 07:21 PM 0
Share

No empty game object.

avatar image DavidDebnar · Jul 10, 2013 at 07:26 PM 1
Share

And is it moved at runtime?

avatar image davidflynn2 · Jul 10, 2013 at 07:28 PM 0
Share

no I have nothing moving that object.

avatar image DavidDebnar · Jul 10, 2013 at 07:30 PM 0
Share

Ok, let me post a changed version of your script then, and see if it works for you or not.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DavidDebnar · Jul 10, 2013 at 07:35 PM

Ok, try this fixed version of your code.

 using UnityEngine;
 using System.Collections;
 
 public class WorldSpawn : MonoBehaviour {
     
     public GameObject block1; 
 
     public int worldWidth  = 10;
     public int worldHeight  = 10;
 
     public float spawnSpeed = 0;
 
     void  Start () {
         StartCoroutine(CreateWorld());
     }
 
     IEnumerator CreateWorld () {
         for(int x = 0; x < worldWidth; x++) {
             yield return new WaitForSeconds(spawnSpeed);
             
             for(int z = 0; z < worldHeight; z++) {                
                 yield return new WaitForSeconds(spawnSpeed);
 
                 GameObject block = Instantiate(block1, Vector3.zero, block1.transform.rotation) as GameObject;
                 block.transform.parent = transform;
                 block.transform.localPosition = new Vector3(x, 0, z);
             }
         }
     }
 }

--David--

Comment
Add comment · Show 2 · 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 davidflynn2 · Jul 10, 2013 at 07:47 PM 0
Share

That fixed it thanks so much.

avatar image JasonC001 · Oct 02, 2014 at 07:16 PM 0
Share

This is awesome stuff :) Thanks

avatar image
0

Answer by tw1st3d · Jul 10, 2013 at 07:29 PM

Well considering you're returning

 yield return new WaitForSeconds(spawnSpeed);

After 0 seconds, it's not going to continue after that.

 return

Means to finish the method.

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 DavidDebnar · Jul 10, 2013 at 07:34 PM 0
Share

No. That's not the cause, and he said that the blocks spawn, but at wrong position.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Object Spawning Too Low 0 Answers

Setting Spawn Rotation correct 1 Answer

Spawning off size blocks 1 Answer

Distribute terrain in zones 3 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