Why can't i set my integer to -1?

Using Javascript, i’ve made a for loop with the condition

i = i + faktor

i’m trying to get faktor a negative value in order for the loop to run backwards but whatever i do e
he won’t let me set it to a value below zero

Code:

var knopfbreite = 75;
var knopfhoehe = 35;



class Dashboard
{
	

   var KMH : GameObject;
   var RPM : GameObject;
   var daten : TextAsset;
   var richtung : int = 4;
   var zeilen : String[];
   var werte : String[];
   var speedwerte : String[];
   var i : int = 4;
   var merkwert: int = i;
   var faktor : int = 1;
  
   
   var kmh_rotation = 0;
   var rpm_rotation = 0;
   
   
   
   function Berechne(faktor : int)
   {
   		zeilen = daten.text.Split("

"[0]); // eine dateizeile pro array-feld

   		for(i=merkwert; i< zeilen.length-1; i = i+faktor)
   		
   		{
   		
   			
   		
   			if(i < 4)
   			{
   				break;
   			}
   		
   		werte = zeilen*.Split(";"[0]); //ein wert pro array-feld*
  •  kmh_rotation = int.Parse(werte[4]); // grobe zuweisung einfacher int-werte*
    
  •  rpm_rotation = int.Parse(werte[8]); // grobe zuweisung einfacher int-werte*
    

kmh_rotation = Mathf.Clamp(kmh_rotation / 300.0 * 180.0, 0.0, 180.0); // umrechnung Absoluter wert → rotationswert
rpm_rotation = Mathf.Clamp(rpm_rotation / 8000.0 * 240.0 , 0.0, 240.0); // umrechnung Absoluter wert → rotationswert

  •  merkwert = i;*
    
  •  print("faktor : " + faktor); // not being executed, but play / fast forward / first value / last value / stop works*
    
  •  if(faktor > 0)*
    
  •  {*
    
  •  yield WaitForSeconds(1.0/faktor);*
    
  •  }else{*
    
  •  	yield WaitForSeconds(1.0);*
    
  •  }*
    
  •  }*
    

}

}

var Objekt : Dashboard;

function Start()
{

}
function Update()
{

switch (Objekt.richtung)
{
case 0 : print(“idle”);
break;

case 1 : print(“Erster Wert!”);

  •  			 Objekt.i = 4;*
    
  •  			 Objekt.merkwert = 4;*
    
  •  			 Objekt.werte = Objekt.zeilen[4].Split(";"[0]);*
    
  •  			 Objekt.kmh_rotation = 0;*
    

Objekt.rpm_rotation = 0;

  •  			 StopAllCoroutines();*
    

break;

case 2 : print(“Rewind!”);

  •   StopAllCoroutines();*
    
  •   Objekt.faktor = -2;*
    
  •   StartCoroutine(Objekt.Berechne(Objekt.faktor));*
    
  •   print(Objekt.faktor);*
    

case 3 : print(“Play!”);

  •   Objekt.faktor = 1;*
    
  •   StopAllCoroutines();*
    
  •   StartCoroutine(Objekt.Berechne(Objekt.faktor));*
    
  •   Objekt.richtung = 0;* 
    

break;

case 4 : print(“Stop!”);
Objekt.richtung = 0;
StopAllCoroutines();
break;

case 5 : print(“Forward!”);
Objekt.richtung = 0;
Objekt.faktor = 5;
StopAllCoroutines();
StartCoroutine(Objekt.Berechne(Objekt.faktor));
break;

case 6 : print(“Letzter Wert!”);

Objekt.i = Objekt.zeilen.length-2;
Objekt.merkwert = Objekt.zeilen.length-2;
Objekt.werte = Objekt.zeilen[Objekt.zeilen.length-2].Split(“;”[0]);
Objekt.kmh_rotation = 0;
Objekt.rpm_rotation = 0;
StopAllCoroutines();

break;

case 7 : Application.Quit();
}

if(Objekt.richtung == 1)
{

  •  	StopAllCoroutines();*
    

Objekt.KMH.transform.rotation = Quaternion(0,0,0,0);

  • Objekt.RPM.transform.rotation = Quaternion(0,0,0,0);*

  • StopAllCoroutines();*
    }

  •  Objekt.KMH.transform.rotation = Quaternion.Lerp(Objekt.KMH.transform.rotation, Quaternion.Euler(0,0,Objekt.kmh_rotation), Time.deltaTime);*
    

Objekt.RPM.transform.rotation = Quaternion.Lerp(Objekt.RPM.transform.rotation, Quaternion.Euler(0,0,Objekt.rpm_rotation), Time.deltaTime);

}

function OnGUI()
{
if(GUI.Button(Rect(10,10,knopfbreite,knopfhoehe),“Start”))
{
Objekt.richtung = 1;
}

if(GUI.Button(Rect(85,10,knopfbreite,knopfhoehe),“Rewind”))
{
Objekt.richtung = 2;
}

if(GUI.Button(Rect(160,10,knopfbreite,knopfhoehe),“Play”))
{
Objekt.richtung = 3;
}

if(GUI.Button(Rect(235,10,knopfbreite,knopfhoehe),“Stop”))
{
Objekt.richtung = 4;
}

if(GUI.Button(Rect(310,10,knopfbreite,knopfhoehe),“Forward”))
{
Objekt.richtung = 5;
}

if(GUI.Button(Rect(385,10,knopfbreite,knopfhoehe),“End”))
{
Objekt.richtung = 6;
}

if(GUI.Button(Rect(460,10,knopfbreite,knopfhoehe),“Quit”))
{
Objekt.richtung = 7;
}
}
any help is appreciated
thanks in advance
bio

It’s unclear from the snippet you posted why you’re having trouble setting faktor to something negative, but if the purpose is to iterate backwards instead of forwards, you can simply start out at the array’s length-1 (or whatever it is you’re iterating through) and then subtract from the iterator instead of adding to it. Like this:

int[] someArray = new int[20];

for (int i = someArray.Length-1; i >= 0; i--)
{
    // Access elements backwards
}

Does that help?

I solved the issue.

Since i am adjusting the factor of speed in a TextField, i’m setting factor using int.Parse on the input of the textfield.

For the rewind function, i simply invert the result, making both forward and rewind using the same input field and making the behaviour depend on which button is pressed to interpret the input.

Here’s the code :

var knopfbreite = 75;
var knopfhoehe = 35;



class Dashboard
{
	

   var KMH : GameObject;
   var RPM : GameObject;
   var daten : TextAsset;
   var richtung : int = 4;
   var zeilen : String[];
   var werte : String[];
   var speedwerte : String[];
   var i : int = 4;
   var merkwert: int = i;
   var eingabestring : String;
   var faktor : int = 0;
   var kmh_rotation = 0;
   var rpm_rotation = 0;
   
   
   
   function Berechne(faktor : int)
   {
   		zeilen = daten.text.Split("

"[0]); // eine dateizeile pro array-feld

   		for(i=merkwert; i< zeilen.length-1; i = i+faktor)
   		
   		{
   			
   			Debug.Log(i);
   		
   			
   		
   			if(i < 4)
   			{
   				break;
   			}
   		
   		werte = zeilen*.Split(";"[0]); //ein wert pro array-feld*
  •  kmh_rotation = int.Parse(werte[4]); // grobe zuweisung einfacher int-werte*
    
  •  rpm_rotation = int.Parse(werte[8]); // grobe zuweisung einfacher int-werte*
    

kmh_rotation = Mathf.Clamp(kmh_rotation / 300.0 * 180.0, 0.0, 180.0); // umrechnung Absoluter wert → rotationswert
rpm_rotation = Mathf.Clamp(rpm_rotation / 8000.0 * 240.0 , 0.0, 240.0); // umrechnung Absoluter wert → rotationswert

  •  merkwert = i;*
    
  •  if(faktor > 0)*
    
  •  {*
    
  •  yield WaitForSeconds(1.0/faktor);*
    
  •  }else{*
    
  •  	yield WaitForSeconds(1.0);*
    
  •  }*
    
  •  }*
    

}

}

var Objekt : Dashboard;

function Start()
{

}
function Update()
{

switch (Objekt.richtung)
{
case 0 : print(“idle”);
break;

case 1 : print(“Erster Wert!”);

  •  			 Objekt.i = 4;*
    
  •  			 Objekt.merkwert = 4;*
    
  •  			 Objekt.werte = Objekt.zeilen[4].Split(";"[0]);*
    
  •  			 Objekt.kmh_rotation = 0;*
    

Objekt.rpm_rotation = 0;

  •  			 StopAllCoroutines();*
    

break;

case 2 : print(“Rewind!”);

  •   Objekt.richtung = 0;*
    
  •   StopAllCoroutines();*
    
  •   StartCoroutine(Objekt.Berechne(Objekt.faktor));*
    
  •   print(Objekt.faktor);*
    
  •   break;*
    

case 3 : print(“Play!”);

  •   Objekt.faktor = 1;*
    
  •   StopAllCoroutines();*
    
  •   StartCoroutine(Objekt.Berechne(Objekt.faktor));*
    
  •   Objekt.richtung = 0;* 
    

break;

case 4 : print(“Stop!”);
Objekt.richtung = 0;
StopAllCoroutines();
break;

case 5 : print(“Forward!”);
Objekt.richtung = 0;
StopAllCoroutines();
StartCoroutine(Objekt.Berechne(Objekt.faktor));
break;

case 6 : print(“Letzter Wert!”);
Objekt.i = Objekt.zeilen.length-2;
Objekt.merkwert = Objekt.zeilen.length-2;
Objekt.werte = Objekt.zeilen[Objekt.zeilen.length-2].Split(“;”[0]);
Objekt.kmh_rotation = 0;
Objekt.rpm_rotation = 0;
StopAllCoroutines();
break;

case 7 : Application.Quit();
}
Objekt.KMH.transform.rotation = Quaternion.Lerp(Objekt.KMH.transform.rotation, Quaternion.Euler(0,0,Objekt.kmh_rotation), Time.deltaTimeMathf.Abs(Objekt.faktor));
Objekt.RPM.transform.rotation = Quaternion.Lerp(Objekt.RPM.transform.rotation, Quaternion.Euler(0,0,Objekt.rpm_rotation), Time.deltaTimeMathf.Abs(Objekt.faktor));

}

function OnGUI()
{
if(GUI.Button(Rect(120,10,knopfbreite,knopfhoehe),“Start”))
{
Objekt.richtung = 1;
}

if(GUI.Button(Rect(450,50,knopfbreite,knopfhoehe),“Rewind”))
{
Objekt.richtung = 2;
Objekt.faktor = -(int.Parse(Objekt.eingabestring));
}

if(GUI.Button(Rect(230,10,knopfbreite,knopfhoehe),“Play”))
{
Objekt.richtung = 3;
}

if(GUI.Button(Rect(340,10,knopfbreite,knopfhoehe),“Stop”))
{
Objekt.richtung = 4;
}

  •  Objekt.eingabestring =  GUI.TextField(Rect(340,50,knopfbreite,knopfhoehe),Objekt.eingabestring);*
    
  •  if(GUI.Button(Rect(230,50,knopfbreite,knopfhoehe),"Forward"))*
    
  •  {*
    
  •  	Objekt.richtung = 5;*
    
  •  	Objekt.faktor = int.Parse(Objekt.eingabestring);*
    
  •  }*
    

if(GUI.Button(Rect(460,10,knopfbreite,knopfhoehe),“End”))
{
Objekt.richtung = 6;
}

if(GUI.Button(Rect(570,10,knopfbreite,knopfhoehe),“Quit”))
{
Objekt.richtung = 7;
}
}
Maybe someone else needs this.
Thanks to Christian H Pedersen for the push into the right direction.