Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Ok, I swear I'm Decent At Coding But...(SOLVED)

Discussion in 'Scripting' started by keenanwoodall, Dec 11, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I've always typed in Javascript and am now converting to C#. I've used it a bit and have a solid understanding of it...or so I though. I'm setting up a very VERY simple GUI script but I am getting two errors. I hate being "that guy" on the forums who asks for help on something stupid, but here I am :( I know Javascript is more relaxed about what you can do so I'm assuming Unity is throwing up errors because I'm coding in bad form or something. C# forces you to do stuff properly relative to Javascript so that's probably what the problem is.
    These are the errors:
    ----
    Assets/Scripts/Game Scripts/GUI/LevelChanger.cs(13,18): error CS1525: Unexpected symbol `if'

    Assets/Scripts/Game Scripts/GUI/LevelChanger.cs(16,1): error CS8025: Parsing error
    ----
    And the code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LevelChanger : MonoBehaviour
    6. {
    7.     public Rect leftButton = new Rect(0, 0, 10, 10);
    8.     public Rect rightButton = new Rect(0, 0, 10, 10);
    9.  
    10.     public void OnGUI ()
    11.     {
    12.         if(GUI.Button(Rect(leftButton, "<"))
    13.  
    14.         if(GUI.Button(Rect(rightButton), ">"))
    15.  
    16.     }
    17. }
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    you got two ifs but nothing inside em? try
    Code (CSharp):
    1. if(GUI.Button(Rect(leftButton, "<")))
    2.     Debug.Log("Left");
    3. if(GUI.Button(Rect(rightButton, "<")))
    4.     Debug.Log("Right");
    edit: added )'s at ends of lines
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    ^ Still not enough )'s
     
    rakkarage likes this.
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Those if statements don't make sense no matter the language.

    I believe this is what you're after:
    Code (csharp):
    1. if (GUI.Button (leftButton, "<")) {
    2.  
    3. }
    4.  
    5. if (GUI.Button (rightButton, ">")) {
    6.  
    7. }
     
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Ok so I added the Debugs and got about 10 new errors, most of which were for an unexpected symbol...dafug?
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
  8. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Jesus Christ...you're right. It requires a Rect and I gave it one...with a rect inside of it. I guess that'd be why.
     
  9. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Here's the working code...thanks guys lol
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LevelChanger : MonoBehaviour
    6. {
    7.     public Rect leftButton = new Rect(0, 0, 10, 10);
    8.     public Rect rightButton = new Rect(0, 0, 10, 10);
    9.    
    10.     public void OnGUI ()
    11.     {
    12.         if(GUI.Button(leftButton, "<"))
    13.         {
    14.           Debug.Log ("Left");
    15.         }
    16.         if(GUI.Button(rightButton, ">"))
    17.         {
    18.             Debug.Log ("Right");
    19.         }
    20.     }
    21. }