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 /
avatar image
0
Question by biohazard · Jul 22, 2011 at 07:14 AM · javascriptfor-loop

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("\n"[0]); // eine dateizeile pro array-feld
            
            for(i=merkwert; i< zeilen.length-1; i = i+faktor)
            
            {
            
                
            
                if(i < 4)
                {
                    break;
                }
            
            werte = zeilen[i].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

Comment
Add comment · Show 2
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 Mattivc · Jul 22, 2011 at 07:54 AM 0
Share

Its unclear what you are trying to do. Pleas provide the code for the entire loop, ins$$anonymous$$d of just the single line. And it will be a lot easier to help you.

avatar image biohazard · Jul 22, 2011 at 09:41 AM 0
Share

to bump or not to bump?

2 Replies

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

Answer by biohazard · Jul 22, 2011 at 11:11 AM

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("\n"[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[i].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.deltaTime*Mathf.Abs(Objekt.faktor));
         Objekt.RPM.transform.rotation = Quaternion.Lerp(Objekt.RPM.transform.rotation, Quaternion.Euler(0,0,Objekt.rpm_rotation), Time.deltaTime*Mathf.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.

Comment
Add comment · Show 4 · 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 CHPedersen · Jul 22, 2011 at 11:16 AM 0
Share

Excellent. :) Glad you got it working.

avatar image biohazard · Jul 22, 2011 at 11:17 AM 0
Share

wouldn't have managed it without help :d

avatar image Meltdown · Jul 22, 2011 at 12:18 PM 0
Share

Glad you two are getting along now :)

avatar image biohazard · Jul 22, 2011 at 12:55 PM 0
Share

he bumped my question D: armageddon

avatar image
2

Answer by CHPedersen · Jul 22, 2011 at 07:18 AM

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?

Comment
Add comment · Show 27 · 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 biohazard · Jul 22, 2011 at 07:20 AM 0
Share

the for loop is inside a coroutine and i'm trying to set faktor using GUI.Buttons to change the faktor's value.

by making the faktor a global variable and remembering the counter's value in a variable, i can start a new coroutine with old parameters

PLAY, PAUSE, REWIND JUST AS I NEED IT

avatar image biohazard · Jul 22, 2011 at 07:20 AM 0
Share

sorry caps ._.

avatar image biohazard · Jul 22, 2011 at 07:37 AM 0
Share

is there anything special about negative ints?

avatar image CHPedersen · Jul 22, 2011 at 07:39 AM 0
Share

No. The range of a normal int is -2,147,483,648 to 2,147,483,647. Only if the integer is unsigned (uint) does it prevent negative values.

avatar image biohazard · Jul 22, 2011 at 07:42 AM 0
Share

sigh...why doesnt this workkkk :(

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

changing a coroutine's argument at the coroutine's runtime? 1 Answer

What's wrong with this Loop? 2 Answers

[SOLVED]Does this loop actually end? 1 Answer

A node in a childnode? 1 Answer

Issue with yield 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