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 /
  • Help Room /
avatar image
0
Question by SamuraiKitty · Oct 09, 2015 at 10:40 AM · c#instantiatefor

For loop not working

I'm making a game in C#, and the key concept of the game is instantiating platforms vertically. In one of my scripts for managing the instantiation of platforms, I have a for statement that will instantiate 4 initial platforms and the recycling of platforms is done in another script.

     public Transform platPrefabBottom;
     public Transform platPrefabTop;
     List<Transform> prefabList = new List<Transform>();
     int prefabIndex = 1;
 
     public static int platInstNum = 0;
 
     // Use this for initialization
     void Start () {
         prefabList.Add(platPrefabBottom);
         prefabList.Add(platPrefabTop);
 
         for(int i = 0; i < 4; i++)
         {
             Instantiate(prefabList[prefabIndex], new Vector3(0, platInstNum, 0), Quaternion.identity);
             platInstNum = platInstNum + 6;
 
             if(prefabIndex == 1)
             {
                 prefabIndex++;
             }
             else if(prefabIndex == 2)
             {
                 prefabIndex = 1;
             }
 
             print ("done: " + prefabIndex);
         }
     }

As you can see, I have a for statement that is supposed to loop 4 times, but instead only runs through once. I have told the code to print "done" for confirmation, and it only prints once.

Not sure why this is happening, could anyone give me some help? Thanks

Comment
Add comment · Show 4
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 meat5000 ♦ · Oct 09, 2015 at 10:07 AM 0
Share

I noticed a while back that sometimes prints and debug.logs dont work effectively in a for loop. The two functions are particularly slow and often cant keep up with the execution speed of a for loop. Dont rely on this.

Are you not simply instantiating the same objects on top of each other?

avatar image SamuraiKitty meat5000 ♦ · Oct 09, 2015 at 10:15 AM 0
Share

No, I've already checked if I'm instantiating them on top of each other and no they aren't but thanks for the reply.

avatar image meat5000 ♦ SamuraiKitty · Oct 09, 2015 at 10:53 AM 0
Share

It should spawn 2 of each right? 2 vertically placed objects alternating twice, spaced by 6?

Show more comments

1 Reply

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

Answer by Chris-Paul · Oct 09, 2015 at 10:57 AM

Ok so your prefab list has only two elements, the index of the first is 0, and the second element's index is 1, the problem is that you let your "prefabIndex" to reach the value of 2, which it's outside of your list's boundaries, therefore you probably get an error when you try to acces prefabList[2].

This is the correct code:

 public Transform platPrefabBottom;
     public Transform platPrefabTop;
     List<Transform> prefabList = new List<Transform>();
     int prefabIndex = 1;   
     
     public static int platInstNum = 0;
     
     // Use this for initialization
     void Start () {
         prefabList.Add(platPrefabBottom);
         prefabList.Add(platPrefabTop);
         
         for(int i = 0; i < 4; i++)
         {
             Instantiate(prefabList[prefabIndex], new Vector3(0, platInstNum, 0), Quaternion.identity);
             platInstNum = platInstNum + 6;
 
             // This is correct.
             prefabIndex ++;
             if(prefabIndex >= prefabList.Count) {
                 prefabIndex = 0;
             }
             
             print ("done: " + prefabIndex);
         }
     }

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 SamuraiKitty · Oct 09, 2015 at 12:25 PM 0
Share

Oh yeah, I always mess that up by starting arrays at 1 ins$$anonymous$$d of 0! I'll try this code first thing in the morning, thanks. EDIT: Never$$anonymous$$d I tried it now, and it works! Thanks!

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

33 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

Related Questions

How does one continuously move an object forward without using velocity? 1 Answer

Adding different values to randomly created objects 0 Answers

While instantiating getting object reference not set for no reason? 1 Answer

Prefab destroyed upon instantiation 0 Answers

Using One Script for Two Separate Parent GameObjects Failing 0 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