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 Lakish · May 02, 2016 at 03:44 PM · updateclicklogicloopingwhile-loop

why infinit loop ? what im doing wrong?

Hi Community :) I'm trying to code a 3D Ludo in unity5. so I got a working diceroller - with a bool, which get true, if the button is pushed. if the button is pushed and the rigidbody of the dice isSleep() --> then i get a currentDiceValue.

 public void Start()
     {
 
         while(GameObject.Find("gameController").GetComponent<gameController>().SpielerRot && GameObject.Find("Wuerfel").GetComponent<AktuellerWuerfelWert>().aktuellerWert != 6) // Roll the Dice until currentValue is 6
         { 
             if (GameObject.Find("Wuerfel").GetComponent<AktuellerWuerfelWert>().aktuellerWert == 6 ) //Gamestart player moves out of his base
             {
                 GameObject.Find("SpielerR1").transform.position = GameObject.Find("gameController").GetComponent<gameController>().rotesSpielFeld[0]; //player is now on playground
                 Debug.Log("Ende 1. If");
                 //GameObject.Find("gameController").GetComponent<gameController>().SpielerRot = false;
             }
             
             if (GameObject.Find("SpielerR1").transform.position == GameObject.Find("gameController").GetComponent<gameController>().rotesSpielFeld[0] && !GameObject.Find("gameController").GetComponent<gameController>().istgedrueckt) 
             { // If player is out of his base he should if current value is 6 ;) AND the dicerollerbutton is NOT pushed --> cause this function has to wait on a new value
                 
                 if (GameObject.Find("gameController").GetComponent<gameController>().istgedrueckt)
                     //now comes the new value
                 {
                     // he should change his position
                     GameObject.Find("gameController").GetComponent<positionen>().positionswechselR1();
                 }
                 GameObject.Find("gameController").GetComponent<gameController>().SpielerRot = false;
             }
 
         } 
         // And now i would change the player in another color
        
 
     }

Why is here a infit loop.

Is my logic wrong? Can maybe someone help me with this start??

And if I would wrote a Coroutine... I would still use there another while loop until the 6 is rolled.. how should I say it in another way?

@Ali hatem

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

3 Replies

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

Answer by Ali-hatem · May 03, 2016 at 10:33 AM

since you use on click i will use public function & for simplicity i will use my old example & you can edit it as you done before :

 bool outBase;//if player out or not
 int currint;
 public List<Vector3> poslist = new List <Vector3>();
 public int dice;
 Renderer rn;//you need Renderer component to change color
 void Start (){
     rn = GetComponent<Renderer> ();
 }
 public void mov(){
     currint = poslist.FindIndex (d =>d == transform.position);
     dice = Random.Range (1, 7);//edited this see my comment below answer
     if (!outBase) {
         if (dice == 6) {
             outBase = true;
             transform.position = poslist[0];//start position
             rn.material.color = Color.green;//change color when out base
         }
     }
     else {
         transform.position = poslist[currint + dice];
     }
 }

when i tested i noticed i never get 6 even with andom.Range (5, 6); so after some research i found that andom.Range will never return it's max value the max is exclusive cheek this thread randomrange

Comment
Add comment · 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
1

Answer by Torigas · May 02, 2016 at 03:56 PM

Try not using while loops.

while (GameObject.Find("gameController").GetComponent<gameController>().SpielerRot) This basically stays true all the time - At least I don't see where it would change. Basically your code gets stuck here if the following line is false

 if (GameObject.Find("Würfel").GetComponent<AktuellerWürfelWert>().aktuellerWert == 6)

It never even leaves the Update method so nothing in your scene can change anymore. The value will stay that way forever and the while loop will keep checking that line.

One thing you could try would be Coroutines where you can actually use while loops and inside yield return null;

http://docs.unity3d.com/Manual/Coroutines.html

Also since you apparently just want to do something on button press:

https://unity3d.com/learn/tutorials/modules/beginner/scripting/get-button-and-get-key

They explain quite nicely how to do that =)

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 Ali-hatem · May 02, 2016 at 04:13 PM 1
Share

cheek this out Lakish why do you use loop

avatar image
0

Answer by Outliver · May 02, 2016 at 05:07 PM

Actually, if it's the same component in the same GameObject, you can just replace

 GameObject.Find("gameController").GetComponent<gameController>()

with

 this

Second hint: Do NOT use umlauts or special characters in variable names.

As your SpielerRot is always true, it's of course an endless loop. About the inner loop, I can't tell as long as I don't know what rotesSpielFeld is.

Comment
Add comment · 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

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

43 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

Related Questions

If Statement within a while loop only checks the if condition once. 0 Answers

Updating Unity or not when working on a project? 0 Answers

How do you link Input.GetKeyDown in an update function to rigidbody 2D physics in fixed update. Below is the script I have so far with everything in the update function to give an idea of what I want to do. 0 Answers

How to stop a countdown timer and disable/stop everything in the scene? 1 Answer

Need help with Async 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