Search Unity

Official Roll-a-ball Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Apr 17, 2015.

  1. Blockinlick

    Blockinlick

    Joined:
    May 9, 2017
    Posts:
    4
    Hmmm

    Okay, the name of the script is PlayerController.cs and the script also says PlayerController. There was a space between the two words, but removing it didn't work.

    Code (csharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.  
    9.     void Start ()
    10.     }
    11.         rb = GetComponent<Rigidbody>();
    12.     {
    13.    
    14.     private void FixedUpdate()
    15.     {
    16.         float moveHorizontal = Input.GetAxis ("Horizontal");
    17.         float moveVertical = Input.GetAxis ("Vertical");
    18.  
    19.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    20.  
    21.         rb.addforce (movement);
    22.     }
    23. }
    Looks exactly like in the tutorial and the script name matches the class name exactly.
     
    Last edited: May 10, 2017
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    When creating a C# file, the name of the primary class, if it's a monobehaviour and the script name must match.

    If you make a script called "PlayerController" in the Unity editor, it should create a class inside that says:

    Code (csharp):
    1. public class PlayerController : MonoBehaviour
    But if the name of the script and the name of the class don't match, there will be issues.

    So, if you had a script called "PlayerController" in the Unity editor and you edited, changed or pasted a different set of code into the script and had, say:

    Code (csharp):
    1. public class playercontroller : MonoBehaviour
    ... Unity wouldn't like it.

    A common cause of this is while following along with the tutorial, someone created a script called "playercontroller", in all lower case, and then to solve a problem, copied and pasted the "done" script code into the script, the class would be "pascal case" and be called "PlayerController".

    That error could also be true if the class no longer derives from ": MonoBehaviour".

    Can you post you code using code tags?
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Whoops - some sort of lag. Posting that updated the page and I now see Anne has answered and you've replied. Sorry for the confusion.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I would make sure that the script is called "PlayerController.cs" in the editor and capitalization counts.

    I'd also restart at least Unity and Monodevelop, just in case pointing Unity to a new script editor has caused any confusion and lastly, I'd launch MonoDevelop from inside Unity by choosing one of the 'edit script' possibilities (eg: double clicking on the script, selecting the script and using the "edit" button or context menu). This last step will insure that Unity builds all of the necessary files correctly for your script editor.
     
  5. sarahc011

    sarahc011

    Joined:
    May 11, 2017
    Posts:
    1
    I have redone the code for the PlayerController many times and had others check it. The ball in my game will still Screen Shot 2017-05-11 at 9.21.35 AM.png Screen Shot 2017-05-11 at 9.21.35 AM.png not move when I try to control it in Play Mode. Please help, thank you.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    "FixedUpdate" is spelled incorrectly and needs a capital "U".

    Don’t forget C# is CaSe SeNsItIvE! So:

    start is not Start
    update is not Update
    Fixedupdate is not FixedUpdate​

    You must make sure you not only use the correct spelling but the correct Casing or Capitalization.
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    "FixedUpdate" is spelled incorrectly and needs a capital "U".
    Don’t forget C# is CaSe SeNsItIvE!
    So:

    start is not Start
    update is not Update
    Fixedupdate is not FixedUpdate​

    You must make sure you not only use the correct spelling but the correct Casing or Capitalization.
     
  8. MOMOcross

    MOMOcross

    Joined:
    May 10, 2017
    Posts:
    1
    Hi I'm following the Unity roll a ball tutorial.

    Screen Shot 2017-05-11 at 1.00.58 PM.png

    Screen Shot 2017-05-11 at 1.01.25 PM.png

    The moving camera view isn't working.

    Please help me. thank you.
     
  9. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    MOMOcross,

    looking at your inspector image I do not see your cameracontroller script attached to it.

    Attach the script, then make sure you drag you player object into the slot for player on the script in the inspector and that should make it work.
     
  10. Allister_Fiend

    Allister_Fiend

    Joined:
    May 16, 2017
    Posts:
    1
    Hello
    I am trying to make the Roll a Ball Game but Unity keeps telling me "The referenced script on this Behaviour (Game Object 'Playa') is missing!" so I can't move the ball or modify the speed.
    Here's what I programed in:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start ()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     void FixedUpdate ()
    17.     {
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    22.  
    23.         rb.AddForce(movement * speed);
    24.     }
    25. }
    I don't know what to do and the tutorials don't say anything about that.
    I'm confused

    [code tags added - mod]
     
    Last edited by a moderator: Sep 11, 2017
    kidcoder likes this.
  11. Deleted User

    Deleted User

    Guest

  12. Ryeath

    Ryeath

    Joined:
    Dec 4, 2016
    Posts:
    265
    Allister, make sure your script names and your class names match exactly.

    If your script is named playercontroller, but your class is PlayerController, that could cause an error like yours.
     
  13. trusth

    trusth

    Joined:
    Apr 16, 2016
    Posts:
    6
    Hi,

    I'm in section 1 of the tutorial and get errors after putting code in and cannot enter play mode.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.  
    8.     private Rigidbody rb;
    9.  
    10.     void Start () {
    11.         rb = GetComponent<Rigidbody> ();
    12.     }
    13.     void FixedUpdate () {
    14.         float moveHorizontal = Input.GetAxis {"Horizontal"};
    15.         float moveVertical = Input.GetAxis {"Vertical"};
    16.  
    17.         Vector3 movement = new Vector3 {moveHorizontal, 0.0f, moveVertical};
    18.         rb.AddForce {movement * speed};
    19.     }
    20. }
     

    Attached Files:

  14. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Replace the brackets {} with parenthesis ()
    Code (csharp):
    1.  
    2.         float moveHorizontal = Input.GetAxis("Horizontal");
    3.         float moveVertical = Input.GetAxis("Vertical");
    4.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    5.         rb.AddForce(movement * speed);
    6.  
     
  15. trusth

    trusth

    Joined:
    Apr 16, 2016
    Posts:
    6
    Parenthesis worked well.
    But can't move player
     
  16. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    There are several reasons this could be happening.

    So, to reason 1, does your script compile correctly?
    Reason 2, does does your script run correctly?

    Check the very bottom of the Unity editor frame for last error message, or,
    better yet, check the Console window for any messages.
    If the script isn't running, neither will the player.

    (To bring up the console window, if it isn't already visible, go to the "Window" tab and click on "Console" at the bottom of the list)
     
  17. trusth

    trusth

    Joined:
    Apr 16, 2016
    Posts:
    6
    unity says metal editor support disabled, skipping device initialization
     
  18. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
  19. trusth

    trusth

    Joined:
    Apr 16, 2016
    Posts:
    6
    Solved I redid tutorial and worked.
    Thanks for all your help.
     
  20. Sledgehammer335

    Sledgehammer335

    Joined:
    Mar 17, 2017
    Posts:
    2
    hey folks got a problem. i cant pick up my cubes when the ball roll's over them all i get is UnityException: Tag: pickup is not defined? know what it means?
     
  21. kidcoder

    kidcoder

    Joined:
    May 18, 2017
    Posts:
    2
    I have same problem like you. I don't know why the ball don't move :(
     
  22. kidcoder

    kidcoder

    Joined:
    May 18, 2017
    Posts:
    2
    I added two line code debug in my PlayerController. Then i see that all show is "I am alive and H (or V) : 0" Only 0.
    Why is it like that? Please help me!!!
    Thank you so much!
     
  23. trusth

    trusth

    Joined:
    Apr 16, 2016
    Posts:
    6
    I have gotten to end game and my script comes with an error on line 41 Unexpected symbol void
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     public float speed;
    9.     public Text countText;
    10.     public Text winText;
    11.  
    12.     private Rigidbody rb;
    13.     private int count;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         rb = GetComponent<Rigidbody>();
    18.         count = 0;
    19.         SetCountText ();
    20.         winText.text = "";
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void FixedUpdate () {
    25.         float moveHorizontal = Input.GetAxis ("Horizontal");
    26.         float moveVertical = Input.GetAxis ("Vertical");
    27.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    28.         rb.AddForce (movement * speed);
    29.     }
    30.  
    31.     void OnTriggerEnter(Collider other) {
    32.         if (other.gameObject.CompareTag ("Pick Up"))
    33.             other.gameObject.SetActive (false);
    34.         count = count + 1;
    35.         SetCountText ();
    36.  
    37.     }
    38. }
    39.  
    40.  
    41.     void SetCountText()
    42. {
    43.         countText.text = "Count: " + count.ToString ();
    44.         if (count >= 12)
    45.     {
    46.         winText.text = "You Win!";
    47.     }
    48. }
     
  24. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    I think you have a misplaced bracket; the closing bracket at line 38 ends the class the was opened at line 6.
    SO, the method SetCountTest at line 41 is not in a good place (not in the class)
    Delete the } at line 38 and add it back in after line 48.
    You will have 3 closing brackets at the ebd of the script:
    line 47 closes the if started at line 45
    line 48 closes the method started at line 42
    line 49 closes the class started at line 6
     
    Last edited: May 18, 2017
  25. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    check the spelling and syntax of the tag name of the prefab with that in the script

    in unity->Project window, Assets->prefab folder will be the cube. It may be named "Pickup"
    When you select it, the inspector will show you a tag name.
    In my case, my tag name is "Pick Up" (capital P, Capital U, space between the words)

    In the void OnTriggerEnter(Collider other) method in the PlayerController.cs script
    is the C# line:
    if (other.gameObject.CompareTag("Pick Up"))

    Note that the string in CompareTag is "Pick Up" and is spelled EXACTLY the same as the tag name in Unity
    (capital P, Capital U, space between the words)
     
  26. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Have you set a value for speed in the playercontroller in the inspector?
    For now, you might want to temporarily change line 6 from
    public float speed;
    to
    public float speed=1.0f;
    and see if you get any movement.
     
  27. trusth

    trusth

    Joined:
    Apr 16, 2016
    Posts:
    6

    Solved thanks
     
  28. Yunzhongzi

    Yunzhongzi

    Joined:
    May 10, 2016
    Posts:
    2
    Thanks for this cool assignment for beginners.
    In the section 1.02, Moving the Player, there are no Transcripts, it is bad for the non-English beginners just like me, could you please add Transcripts ? Thanks a lot!
     
  29. Deleted User

    Deleted User

    Guest

    Put the subtitles on.
     
  30. modern_moron

    modern_moron

    Joined:
    May 21, 2017
    Posts:
    1
    Hey, very new to all of this. I'm around the 5 minute mark on the "Moving the Player" video tutorial and I can't figure out what's going on.

    The tutorial is apparently done in iOS and I'm using Windows 7. Opening the PlayerController script for editing starts up Microsoft Visual Studio where highlighting

    Code (CSharp):
    1. Input
    and using the CRTL + ' command opens a similar looking "Team Explorer" window, but none of the scripting info from Unity appears there as it does in the tutorial.

    It seems to want me to create a whole new Microsoft Visual Studio account to gain access to any of the scripting info.

    Do I just have to do it, or is something not right here? Maybe I skipped an earlier bit of preparation because it seems a bit counter-intuitive to have to go through an entirely separate registration process in the middle of a tutorial.
     
  31. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    This specific setting is more to do with your script editor of choice.

    The installation of unity is packaged with the default script editor called MonoBehaviour which is used in the tutorials you see during the lessons.
    But during the installation you have an option to install Visual Studio as an optional script editor, and when installed, this becomes the default script editor.

    The shortcut keys you use to bring up the scripting reference have a different behaviour in visual studio (can be changed)

    But as a suggestion to work along with video lessons and see what Adam sees I would recommend changing the default script editor back to MonoBehaviour until you feel more comfortable.

    This can be done in unity by
    Edit > preferences > External tools
    And change the script editor to Monobehavior
     
  32. CTGSProductions

    CTGSProductions

    Joined:
    May 23, 2017
    Posts:
    1
    I have a question about how to make the ball move! I have looked at previous comment in this thread but nothing seems to work.
     
  33. Deleted User

    Deleted User

    Guest

    Just replay the video and do as the instructor says; make sure you do not overlook anything and it will work.
     
  34. Halo_Liberation

    Halo_Liberation

    Joined:
    Mar 6, 2017
    Posts:
    26
    Im a bloody idiot. I can not for the life of me figure this out. As pathetic as this might sound i cant get the ball to move.

    I have use Gravity checked and made sure to uncheck "is Kinematic" and even switch between the 2 types, and have the speed set for 10 or more and even double checked the script and ran it through the mono develop bug tester like 15 times and it checks out perfect. Why isnt the ball moving? What more does it want? I managed to do this once but now i am unsure of what i did. Can anyone give any advise?
     
  35. Deleted User

    Deleted User

    Guest

    Could you please post:
    Are you sure you push the right controls when you try to move the player?
     
  36. Khanadian

    Khanadian

    Joined:
    Jun 1, 2017
    Posts:
    2
    Hey, dunno if anyone else has this problem, but when I build the game, the ball goes through the pickups without collecting them. It works perfectly fine when testing in the engine, but when actually playing the build, it's not working.
     
  37. Halo_Liberation

    Halo_Liberation

    Joined:
    Mar 6, 2017
    Posts:
    26
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.  
    8.     private Rigidbody rb;
    9.  
    10.     void Start ()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.     void Fixedupdate ()
    16.     {
    17.         float moveHorizontal = Input.GetAxis ("Horizontal");
    18.         float moveVertical = Input.GetAxis ("Vertical");
    19.  
    20.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    21.  
    22.         rb.AddForce (movement * speed);
    23.     }
    24. }
    this is a picture of my main screen and the code is exact if i followed everything to a tee. i have the speed option, set it for 10 and unchecked "is kinematic" and its still not moving, also the keys i have tried are the standard wsad and arrows, none worked, i even tried the number pad, still nothing sadly. any advice?
     

    Attached Files:

  38. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Line 15, capitalize "u" in Update:
    void FixedUpdate ()
     
  39. Miraul

    Miraul

    Joined:
    Jun 3, 2017
    Posts:
    1
    [
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start()
    12.  
    13.     {
    14.  
    15.         rb = GetComponent<Rigidbody>();
    16.  
    17.     }
    18.  
    19.     void FixedUpdate()
    20.  
    21.     {
    22.  
    23.         float moveHorizontal = Input.GetAxis("Horizontal");
    24.         float moveVertical = Input.GetAxis("Vertical");
    25.  
    26.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    27.  
    28.         rb.AddForce(movement * speed);
    29.     }
    30. }
    WTF.jpg

    I am very close to burn my new laptop, please help me.

    I cant add speed properties to the Player's Inspector Page..
     
    Last edited: Jun 3, 2017
  40. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    I don't think you have attached your PlayerController script to your player.
     
  41. Halo_Liberation

    Halo_Liberation

    Joined:
    Mar 6, 2017
    Posts:
    26
    I restarted from scratch and got it working. Everything is perfect but i do ave one last question. How would i go about adding sounds to the game? Like if i wanted a bell chime to play as the pick up tone and a win song to play after you collect all the items?
     
  42. SushiDev

    SushiDev

    Joined:
    Jun 7, 2017
    Posts:
    1
    Um...I'm on the 'moving the player' one, new to Unity and C#, and can't for the life of me get the ball to move. Can I get some help? Is it because I built this on top of the project with the links to the tutorial? Screenshots below.

     
  43. MotherRussia161

    MotherRussia161

    Joined:
    Jun 7, 2017
    Posts:
    1
    I just started the tutorial and Finished the "Moving the Player" Part but when i try to start the game i just fall trough the ground and i can't find a solution to fix that.
     
  44. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,

    What you can do is have a look at how AudioSources and Listeners work, and how to get them to play with triggers.
    Might be worth having a look through the audio section on the Learn section. or have a look at Matts live training on audio and scripting for some good info.
    https://unity3d.com/learn/tutorials/topics/audio/sound-effects-scripting?playlist=17096

    or, move onto the Space Shooter series, as audio and SFX are used during that series.
    https://unity3d.com/learn/tutorials/projects/space-shooter-tutorial

    Bear in mind when working through the Space Shooter, be sure to have annotations on, and read the docs linked at the top of that page
     
  45. Strayk

    Strayk

    Joined:
    Jun 11, 2017
    Posts:
    1
    I'm encountering a bug where when I try to save my scene, it tells me I have to save it to my assets folder yet that's where I'm trying to save it...
     
    GrimGriefer likes this.
  46. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon,

    Just to guess, what I would do it raise the ball higher off the floor, just to make sure its still not penetrating.

    double check that the floor and the ball have colliders that are not set to be triggers.
    and also check that the player has a Rigidbody component attached.
     
  47. assetstoreunity

    assetstoreunity

    Joined:
    Jun 18, 2017
    Posts:
    1
    (tutorial 3)

    How to quickly zoom out like at 0:36?
     
    Last edited: Jun 21, 2017
  48. kakkarotssj

    kakkarotssj

    Joined:
    Jun 25, 2017
    Posts:
    2
    Why my game view looks like this and how can I fix it look like similar to that in the tutorial
    Please help me find a solution quickly .
    Thanks
     

    Attached Files:

  49. Lamb7

    Lamb7

    Joined:
    Jun 26, 2017
    Posts:
    5
    Hello its telling me that rb does not exist and I just now got into coding and other things like that so I dont know what to do when it says rb does not exist or anything else does not exist
     
  50. Lamb7

    Lamb7

    Joined:
    Jun 26, 2017
    Posts:
    5
    Is it either outdated our am I doing it wrong?