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 Brayan11455 · Apr 17, 2021 at 03:45 PM · loopfor

Problem with for infinitely looping

Hi, can someone help me with this for loop that is infinitely looping the engine when every time I press play, what's wrong with it? Thank you so much.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RigSystem : MonoBehaviour
 {
     [Header("Levels")]
     [SerializeField] private GameObject gameSystem;
     [SerializeField] private int rigLevel = 1;
     private Material[] localMaterialLevel = new Material[12];
 
     [Header("Rigs")]
     [SerializeField] private GameObject objectColor;
     [SerializeField] private GameObject fanColor;
 
     private void Start()
     {
         for (int i = 1; i <= rigLevel; i++)
         {
             if (i == rigLevel)
             {
                 objectColor.GetComponent<Renderer>().material = localMaterialLevel[(i -= 1)];
 
                 if (rigLevel < 5)
                 {
                     fanColor.GetComponent<Renderer>().material = localMaterialLevel[10];
                 }
 
                 else
                 {
                     fanColor.GetComponent<Renderer>().material = localMaterialLevel[11];
                 }
             }
         }
     }
 }

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 Bunny83 · Apr 17, 2021 at 05:05 PM

First of all your for loop it completely pointless since the only code that is inside the loop is executed when i == rigLebel. So why do you have a loop in the first place? Though your main issue is that you have an i -=1 inside your code. So you are manipulating your for loop variable. Which means you will get stuck at this value since when i == rigLevel you subract 1 from i and the for loop is adding 1 at each iteration. Therefore i will stay at that value and you never leave your loop.

As I said, what's the point of the for loop in the first place?

You could replace your Start method with

 void Start()
 {
     objectColor.GetComponent<Renderer>().material = localMaterialLevel[rigLevel - 1];
     if (rigLevel < 5)
     {
         fanColor.GetComponent<Renderer>().material = localMaterialLevel[10];
     }
     else
     {
         fanColor.GetComponent<Renderer>().material = localMaterialLevel[11];
     }
 }

This would have the same effect, just without the pointless loop. Though in general you should never change the loop variable inside the loop body. There are some rare situations where this may be neccessary, but you should be aware of the implications when you do so. In such a case it would be better to use a while loop instead.

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 Brayan11455 · Apr 17, 2021 at 05:21 PM 0
Share

Now I understand

I was thinking to add 10 if statements to check the level and asign the material depending of the level but as you say that is pointless and just putting it in the void Start has the same effect, I didn't realize that xD

Thank you so much

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

115 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

Related Questions

"for" loop - DrawTexture problem. 0 Answers

For loop C#? 2 Answers

Using for as while. 3 Answers

Shorten code with FOR loop 1 Answer

[Solved]Using multiple arrays in one statement? 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