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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

The movement code for my 2d character on unity 5 is all wrong!!!!!! Please help me!!!!!!

Discussion in 'Getting Started' started by picto_3, Aug 12, 2015.

  1. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    I have attached a file that has my script. Please tell me whats wrong and if possible please send me a proper code. Thanks you

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class playercontroller : MonoBehaviour {
    4.     public float moveSpeed;
    5.     public float jumpHeight;
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.         if (Input.GetKeyDown (KeyCode.Space))
    14.         {
    15.             GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight)
    16.         }
    17.  
    18.     }
     
    Last edited: Aug 13, 2015
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,071
    hi,

    how it is wrong / broken?
     
  3. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Indeed, please don't make us guess. Provide as much information as you can to pinpoint the problem.
     
  4. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    well unity gives me two errors, one is a "parser" error and the other one is an unexpected symbol ,"}"
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    those errors will help you debug the code. Parse errors are usually caused by misplaced/missing semi-colons, braces, brackets etc. Start by clicking on the very last } in your script & see what opposite one is highlighted. It should be the very first one in your script. Work your way through it until all your {} are aligned & matched correctly then try your script again. If you have other errors then see what the message is telling you (it usually gives the line the problem is with or double click the message & it will take you to the line that caused the error (it may not be the line where the actual error is though)) & work through it again.
     
    picto_3 likes this.
  6. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    can you please de bug it i have attached the file to this thread and please take a look on what i did wrong
     
  7. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    It says no monobehaviour in file or names dont match
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    No, you need to learn or you will get stuck again with your next script. Debugging is something everyone has to do.
    I also don't open attachments, especially google drive as last time I did someone tried to hack my google account as it was a fake document login page.

    You can post the code in here by using the code tags
     
    picto_3 likes this.
  9. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    i have in total 3 codes and all have the error, no monobehaviour script in them. i dont know how to post it and btw im only 13 years old and am new to coding
     
  10. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    This is the code i was talking about, i have 2 more which have the same error (no monobehaviour script or name of file does not match)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playercontroller : MonoBehaviour {
    5.  
    6.     public float moveSpeed;
    7.     public float jumpHeight;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.  
    17.         if (Input.GetKeyDown (KeyCode.Space))
    18.         {
    19.             GetComponent<Rigidbody2D>().velocity = new Vector2(0, jumpHeight)
    20.         }
    21.    
    22.     }
    23. }
     
  11. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,240
    If you post something here you are expected to post the code and make sure you use code tags so we can read it right here without needing to download anything. People don't mind helping, but they aren't going to download random files to help you.

    For syntax errors like this and null reference exceptions you should not post them here. These sorts of errors basically tell us that you need to go learn the basics on your own.

    It does not matter if you are 13, you're posting here and still get the same treatment and respect as everyone else both young and old however you are expected to act like an adult here.
     
  12. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    i just posted the code
     
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
  14. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,240
    If I can't read it from this page then you haven't posted anything.
     
  15. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
  16. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    its right above your first post
     
  17. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    The only attachment/code appears to be the google drive one in The first post, there's no other post.

    Compare your code to the tutorial. The missing monobehaviour could be the formatting (incorrect {} or other error) or incorrect naming.
    If you are doing the tutorial go over it all again to double check you haven't missed anything or made a typo. Formatting & syntax are very important before you even get onto the more complex concepts & reviewing your code is something you will be doing over & over again as no ones code ever works perfectly the first time.
     
  18. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    i have checked it multiple times and i cant find anything. You look very experienced, cant you just quickly go through the code and see whats wrong cuz i cant.
     
  19. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,498
    You're likely to get one more friendly version of the message before someone tells you harshly.

    The whole point of a tutorial is to learn. If you run to the forum at the first sign of trouble and ask -- borderline demand, really -- that others figure out your issues, what have you actually learned?

    As Ted said, the errors you're seeing will point you to exactly where the problem is. If you don't know what the messages mean, Google them. I promise you that anything you'll run into at this stage has been asked and answered a thousand times before across the Internet. If you don't know how to properly Google things, you'll truly be lost forever.

    You also don't seem to listen. We're not interested in downloading your code file and opening it ourselves. Follow the forum rules, and copy-paste your code into a code tag in a post. Then we'll look at it. The way you've shared your code is not an acceptable way of offering us anything.

    Even once you do post your code properly, we likely could just tell you how to change it to make it work, but what will that gain you? Will you suddenly feel comfortable tackling things on your own from that point on?

    I totally get it. You're young and impatient and want to sprint without having to go through the boring crawl phase. But if you can't solve simple problems like this, you have no chance of actually going anywhere with your game development. It's a ton of hard work, despite what you hear.
     
    Martin_H and blizzy like this.
  20. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    your right but the fact is that i have posted my script on the posts above.
     
  21. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,498
    Unfortunately for you, the forum lets us know when edits were made. My post was 19 minutes ago. Your edit to your first post where you added the code in happened 4 minutes ago. :p Once more, because it bears repeating: When asking for help, attitude is everything.

    What are the error messages you're getting? Pasting those here exactly as they appear will indicate where the problem is.
     
  22. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    i edited because this was the second post about my script i have already posted my script on the posts above. I made 3 scripts the one i shared is the one with the errors (a parsing error and an unexpected symbol). One of them has no errors but unity is still not accepting it. It says that there is no monobehaviour script or the file name is different
     
  23. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,498
    Oy.

    What I mean, is paste the error messages from the console log here. That gives us helpful information, like exactly what the problem is, and where the problem is occurring.

    Just out of curiosity, is your file actually named playercontroller.cs? Because if not, that's one of your problems.
     
  24. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    oh, sorry i bet i look pretty stupid right now. I am sure that my file is named playercontrol.cs. I will post the errors later. My powers out and my data is in my descktop
     
  25. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,498
    None of this is a representation of your intelligence. It's your experience. Don't sweat it. Just learn as much as you can constantly, and try to follow advice where appropriate.

    If your file is actually named playercontrol.cs and not playercontroller.cs, that's a problem. Your file should be named the same as your class name. I'm sure there's other things wrong, but we need a more complete picture to get to the root of it.
     
  26. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    it called player controller not playercontroller.cs. Do in have to write ".cs" on every file name??
     
  27. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,498
    I'm saying there's a difference between playercontrol and playercontroller. You don't add the extension yourself, no.
     
  28. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    i coppied the code from the tutorial, yes. But i called my c sharp file player controller.
    ps when i was writing the code there was no space between player and controller
     
  29. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,498
    When following tutorials, you should name things exactly as they do, unless you REALLY know what you're doing. Things like spaces, spelling, and punctuation can be the difference between your script working as planned, or failing to compile, as you're seeing here.

    Go through the tutorial again, and make sure everything matches how they're doing it exactly. I'm confident that if you do, you'll fix all the errors right then and there.
     
    tedthebug likes this.
  30. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    I made three scripts (i didnt make them out from scratch, i got more than 90% of them from tutorials etc). None of them have the errors anymort the buey all have one problem. Every time i click on them, on the top right corner there is a message that says, " No monobehaviour script in this file, or their names do not match the file". All three of them are different tries on a proper movement script. One of them (which gave me the script i posted) gave me a sprite to download but instead of its sprite i used my own but there was no major difference. I just designed my own player and blocks.
     
  31. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570

    If you've never coded before then not copying tutorials EXACTLY will cause problems that you have no experience or knowledge to use when fixing the errors that will inevitably occur. As a couple of people have said now, delete your file & start again, following the tutorial EXACTLY. Naming things is important, if you change one name you can open up a world of pain later in the tutorial.

    I've been coding for 18mths, I know none of the complex or efficient ways to code as I'm still learning the logic concepts to getting my code to do what I want. I've used tutorials, YouTube & blogs to get hints to help me start putting my own changes into the tutorial projects that I built previously by following the tutorials EXACTLY. There are no short cuts to learning to code, if you take them yourself you will suffer & not get any sympathy from others.
     
  32. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    I followed the tutorial in a new file and finally when scripting i got it all right, my character can now jump, i am currently scripting to make my character go left and right
     
  33. picto_3

    picto_3

    Joined:
    Aug 12, 2015
    Posts:
    19
    THANK YOU SO MUCH, BECAUSE OF YOUR ADVISE I MADE A PERFECT SCRIPT THAT OPERATED PERFECTLY
     
    Schneider21 likes this.
  34. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    well done. Now see if you can write a basic one, even if it is just left & right movement, without the tutorial or looking at your previous script. This will help reinforce what you just learnt before you start trying to learn new stuff.
     
unityunity