Search Unity

Why Error ?

Discussion in 'Getting Started' started by badassgamer, Oct 28, 2015.

  1. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    Iam Learning c# , in that iam using if statement but why it keep giving me error when everything is perfect . i learned a lot but while learning its gives me so much errors which sloved myself, well to be honest it gives error even for destroy object but when i retype everything the error gone , i noticed more than 3 times everything is same but why error ? thats why i find it bit annyoing because of it.it even gives error for right statement but when retype it gone.
    here is the error
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestScripts : MonoBehaviour {
    5.  
    6.     int life = 5;
    7.  
    8.     // Use this for initialization
    9.  
    10.     void Start()
    11.     {
    12.  
    13.  
    14.  
    15.  
    16.     }
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23.     // Update is called once per frame
    24.  
    25.     void Update() { }
    26.  
    27.                if (life < 1){
    28.  
    29.                       Debug.Log ("Playing");
    30. }
    31.                   else {
    32.                           Debug.Log ("GameOver");
    33.  
    34.  
    35. }
    36.  
    ..

    Now here is the error
    Assets/TestScripts.cs(27,25): error CS1519: Unexpected symbol `<' in class, struct, or interface member declaration

    Assets/TestScripts.cs(29,33): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration



    Assets/TestScripts.cs(31,22): error CS8025: Parsing error...


    here in this tutorial "
    " he made same like me but it gives me error
     
    Last edited: Oct 28, 2015
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Don't fool yourself into thinking your code is perfect, because that mentality will just get in the way of finding your problem. After all, if it was perfect then there wouldn't be an error message.

    In this case just look at the line number indicated in the error message and then look right before that line for mistakes. When I look at line 27, and then look at the last bit of code before that, I see: void Update() { }

    That closing bracket is a mistake.
     
    Ryiah and badassgamer like this.
  3. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    Perfect means i written exactly same as what written, lol sorry not that meaning and lol this why i said its so strange look another error where it says line (41,1) but my code ends at 36 how can i get error on line 41 where there is nothing written.
    here is edited code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestScripts : MonoBehaviour {
    5.  
    6.     int life = 5;
    7.  
    8.     // Use this for initialization
    9.  
    10.     void Start()
    11.     {
    12.  
    13.  
    14.  
    15.  
    16.     }
    17.  
    18.  
    19.  
    20.  
    21.  
    22.  
    23.     // Update is called once per frame
    24.  
    25.     void Update(){
    26.  
    27.                if (life < 1) {
    28.  
    29.                       Debug.Log ("Playing");
    30. }
    31.                   else {
    32.                           Debug.Log ("GameOver");
    33.  
    34.  
    35. }
    36. }
    37.  
    38.  
    39.  
    40.  
    Here is the error
    Assets/TestScripts.cs(41,1): error CS8025: Parsing error
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Your code is 40 lines long, and it's saying parsing error on line 41, which means it got to the end of the file and was so confused it couldn't figure out what else to say.

    Fix the indentation of your code so that everything lines up nice and neat. And while one brace is highlighted, the other matching brace should be highlighted too; use that to make sure all the braces are paired up correctly. Most likely, you still have an extra brace or a missing brace somewhere. (...and... yes, I matched them up in my head and there is one too few.)
     
    Flipbookee, Ryiah and badassgamer like this.
  5. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    You may not have anything written on those lines, but your code does in fact have a bunch of blank lines at the end, so the compiler needs to parse those too. It looks like the compiler is still trying to look for another closing bracket when it hits the end of the file.

    EDIT: he was faster :p
     
    badassgamer likes this.
  6. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    Yes but its now saying that "Assets/TestScripts.cs(26,10): error CS0116: A namespace can only contain types and namespace declarations" .. but i cant believe so much errors for simple code . this is just 4 or 5 statement code but it give me more errors than statements lol .
     
  7. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    [
    ok sloved myself thanks you both for help , i mean the new error lol
     
  8. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    Also can i use MonoDevelop or VisualStudio for scripting in unity3d, which is better?
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Try them both and see what you prefer.

    But for what it's worth, I just today switched over to Script Inspector 3, and I can't believe how much happier I am. It lets you edit scripts right there within Unity — either right in the inspector panel, or in a separate window with a tab for each script you have open. It even does stuff like lets you jump to any point in the call stack of a console log message (if you don't know understand yet how cool that is, trust me, you will one day!). It's also way, way faster than MonoDevelop, which has been pretty much the only option for me until now (since I'm on a Mac). I love it!
     
  10. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    ok I will try Script Inspector if its on windows . that video which i point out on my question , is the tutor in the video made 21 videos about scripting , on a last video the tiltle says OnCollsionEnter2D but so he teaches for only 2d games or both 2d and 3d
     
  11. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    What Wrong with the word "Width" in code why it giving error
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayersHealthTestingPurposes : MonoBehaviour
    5. {
    6.     public int maxHealth = 100;
    7.     public int curHealth = 100;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.  
    21.  
    22.     void OnGUI() {
    23. GUI.Box(new Rect ( 10, 10, Screen.Width/2 / (maxHealth /curHealth) , 20) , curHealth + "/" + maxHealth);
    24.     }
    25.  
    26. }
    errors
    Assets/Standard Assets/Characters/RollerBall/Scripts/PlayersHealthTestingPurposes.cs(24,35): error CS0117: `UnityEngine.Screen' does not contain a definition for `Width'

    Assets/Standard Assets/Characters/RollerBall/Scripts/PlayersHealthTestingPurposes.cs(24,74): error CS1502: The best overloaded method match for `UnityEngine.Rect.Rect(float, float, float, float)' has some invalid arguments

    Assets/Standard Assets/Characters/RollerBall/Scripts/PlayersHealthTestingPurposes.cs(24,5): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Rec
     
  12. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    This thread is a great example of the "Give a man a fish and he'll eat for a day. Teach a man to fish and he'll eat for a lifetime." proverb is so true.

    Your errors are all fairly simple, and the error messages you're receiving are telling you exactly what's going wrong. You lack a fundamental understanding of programming that you're not going to learn by people solving these for you.

    It should go without saying that spelling, capitalization, punctuation... all of that stuff is extremely important when writing code. For example, at least one of your current errors is due to a capitalization error.

    Learn to write code. Then learn to read documentation. Looking at the documentation for the Screen object, for example, will indicate why your code says it has no definition for "Width".
     
    Ryiah, badassgamer and Dantus like this.
  13. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    Understood But sometime its so confusing that even if it is simple its so hard to find . i cant understand what it telling like for example "`UnityEngine.Rect.Rect(float, float, float, float)'" for simple capitalisation . but alteast "does not contain a definition for `Width'" make some senses but what about this Rect.Rect(float, float, float, float what this really means lol and thanks for help sloved anyway
     
  14. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Errors tend to cascade (one error causes another, etc). Mostly focus on the first one, and see if the others go away when you fix that.
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    In live training (see the learn tab above) there are at least 3 live training sessions on introduction to coding. I'd suggest the one indie called (something like) coding for the absolute beginner.

    Sadly code isn't magic. (Tho inscribing my code on a marble rock face and having it glow with some ethereal cold fire would be cool as heck!) Code is just logic, and we have to follow these rules EXACTLY. We can't just throw a bunch of brackets or braces at the screen as the gibberish we see in other people's code is full of them. We need to understand that the scope of logic is contained by these symbols {}.

    Everything means something with code. If you put anything down on the "page" in your code editor and you don't know what it means or why it's there - don't wait for an error, google it or asks about it. If you don't understand anything, then start with that.

    What will annoy people is if you ask them to solve one console error at a time without learning from it.
     
    Ryiah and Schneider21 like this.
  16. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I read something, & am unfortunately learning for myself, that coding is like writing instructions for an idiot. You can't just tell a computer to do something, you have to tell it what to use, when to use it & give those instructions in the exact order they need to be done as it can only do one thing at a time & will never know what it is going to be doing in the next step until it gets to it.
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    That's because computers are idiots that do exactly what you say in the order that you say it!
     
    Ryiah likes this.
  18. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    Wow. I'm surprised no one has ever mentioned this before in those IDE discussion threads.
     
    Flipbookee likes this.
  19. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    A little while back, I switched to Xamarin Studio, which I'm very happy with. I haven't tried Script Inspector, though, so I can't compare them directly.
     
  20. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I use Xamarin Studio on other (non-Unity) projects, and it's OK, but not great. Just this week I've been using it quite a lot on a very hush-hush top-secret project that I can't talk about yet, even though it eventually will be for Unity... but for now it's in Xamarin.

    And I gotta say, that's one annoying editor. It keeps replacing the perfectly correct code I typed with different code that is not correct. So instead of typing what I want, I often have to type what I want, curse, hit undo, and continue. Or sometimes I type what I want, curse, hit delete a few types, type what I want again (and sometimes repeat — I haven't yet figured out why sometimes this approach works and sometimes it just replaces my correct code with incorrect code again).

    Rule #1 for any editor should be: don't change what the user has typed unless you are absolutely, positively, 100% sure you're right and the user is wrong. Xamarin and MonoDevelop both break this rule frequently.

    Si3 has autocomplete (intellisense, whatever you want to call it), but so far, it's never broken this rule for me. When I type what I want, I get what I typed. When I want help, the help is there. It's the way an editor should be.

    I only hope that Unity drives a big truckful of cash onto Flipbookee's driveway, dumps the load, and incorporates Si6 into Unity 6. After using it for a week, it now pains me to think of anyone trying Unity without it.
     
    Flipbookee and Ryiah like this.
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Not to hijack the thread but, @Flipbookee's plugin is what I use on all my personal projects. I'm not able to use it on my official projects, sadly. It's great and you don't have to power up another application.
     
    Flipbookee, Ryiah and JoeStrout like this.
  22. Flipbookee

    Flipbookee

    Joined:
    Jun 2, 2012
    Posts:
    2,798