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
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
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 =)
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.
Your answer
