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 upariver · Apr 25, 2016 at 02:46 PM · c#for loop

For Loop not working

My for loop is not working and after checking Unity documentation and forums I have not found a solution. Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour {
 
     public int numPlat;
     
     public GameObject PlatformToSpawn;
     
     // Use this for initialization
     void Start () {
         
         Debug.Log("Number" + numPlat);
         spawnStuff();
         
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void spawnStuff()
     {
         for (int i = 0; i > numPlat; i++)
         {
             Debug.Log("ds");
         }
     }
 
 }
Comment
Add comment · Show 3
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 Riderfan · Apr 25, 2016 at 03:06 PM 1
Share

Looks like your itteration logic is backwards.

try;

 for (int i = 0; i  <= numPlat; i++)

this will itterate from 0 to whatnever number numPlat happens to be.

avatar image upariver Riderfan · Apr 25, 2016 at 03:20 PM 0
Share

So, having this would not work? $$anonymous$$ust the condition always be smaller than the variable? $$anonymous$$any thanks for answering.

avatar image Riderfan upariver · Apr 25, 2016 at 03:27 PM 0
Share

If you want to count up, from something to something greater, then this is how you write the logic.

What you had was the itterator testing to see if it was greater than your variable (which you never set as anything so it was always zero anyway). So that condition meant it could never enter the loop.

1 Reply

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

Answer by Ali-hatem · Apr 25, 2016 at 03:06 PM

 for (int i = 0; i < numPlat; i++) you need <sign not  >sign
Comment
Add comment · Show 3 · 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 Riderfan · Apr 25, 2016 at 03:28 PM 0
Share

That logic will finish the loop 1 itteration short. You need to have <= to ensure that you're going all the way to the variable value.

avatar image Ali-hatem Riderfan · Apr 25, 2016 at 06:53 PM 0
Share

@Riderfan how is that if numPlat = 10 loop will keep ruining until i is not < numPlat any more & because of increasing i the loop will stop after 10 times otherwise the loop will run to infinity & the reason i used i< numPlat not i<= numPlat because the zero will be count & if you want to instantiate 10 objects then with i<= numPlat will instantiate 11 objects so with simple debug every thing will be clear

 int numPlat = 10;
 void Start(){
     for(int i = 0; i<numPlat; i++){
         print (i);
     }
 }

the result will be printing the i teen times from 0 to 9 @upariver hop this will explain every thing to you;

avatar image upariver · Apr 25, 2016 at 03:40 PM 0
Share

So is it a rule that i must never be higher than the var, otherwise it doesn't run? $$anonymous$$any thanks for answering.

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

148 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 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 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 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 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

Using foreach to remove and delete bullet in List - C# 3 Answers

start position instantiate for loop. c# 1 Answer

3 for loops and a IF crashes when the if is true 1 Answer

simplest way to add a minimum distance or gap between instantiated objects 1 Answer

For Loop Problem 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