Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Official Roll-a-ball Tutorial Q&A

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

  1. RavenLiquid

    RavenLiquid

    Joined:
    May 5, 2015
    Posts:
    44
    You could set a breakpoint at line 21 for instance. That way the game will stop when it reaches that line and allow you to inspect the properties and variables.

    It will stop there almost instantly every time because it is set in the update method, but it should allow you to see at least if the values make sense (check if speed is not 0 for instance). Horizontal and vertical will be 0 as you cannot apply input while the game is breaked (and it will break before you can do anything).

    If the rigidboy is not attached, the GetComponent will return null and the addforce call will crash because you are trying to call a method on something that does not exist so I think that is setup correctly.

    And just to be sure, you know you have to actually move the ball with the controlls set up for the input axes? (WASD should work).
     
  2. DazedAndConfused

    DazedAndConfused

    Joined:
    May 8, 2015
    Posts:
    3
    Ok, thank you, I've got it working, thank you, the feedback here is really good compared to other forums, thanks
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you checked that you've not accidentally locked any of the axes on the rigidbody? This can prevent the ball from rolling.
     
  4. Zamas

    Zamas

    Joined:
    Apr 15, 2015
    Posts:
    35
    A public variable does not mean you absolutly have to publicly initiate it. You can do it like in his script without problem. When a variable is public, it's means that she's not restricted to her class.

    Did you try adding * Time.deltaTime to your rb.addForce ?
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    It may be that the UI Text element does not have the area to show all the text. Try either make the UI text element larger, or change the wrap made so that it's not constrained by the boundaries of the UI Text's area.

    To stop the timer, an easy to code solution would be to wrap it in an "if" checking gameOver:
    Code (csharp):
    1. if (!gameOver) {
    2.  
    3. }
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please learn to format your code using code tags.
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What you want to do is affect the value of time by multiplying it by a smaller value:
    Code (csharp):
    1. transform.localScale = new Vector3(Mathf.PingPong(Time.time * 0.5f, ... ...
    For ease, you could declare a variable, like "scaleValue":
    Code (csharp):
    1. public float scaleValue;
    ... And using that instead of a hard value:
    Code (csharp):
    1. transform.localScale = new Vector3(Mathf.PingPong(Time.time * scaleValue, ... ...
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Hmm. If you feel the placement of the text is in error, please fill out a bug report, and get me the number.

    That being said, you should be able to set the values on the RectTransform to match the tutorial. As a good starting place, you could use the reset function on the context sensitive gear menu to reset the RectTransform's values. This will completely reset all values, so make sure to duplicate all of the values.
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you getting any errors at all in the console when you try to play the scene?


    When you declare a variable public, it is editable in the inspector. In this case, using "= 100.0f;", simply sets the default value for that variable to 100. This value will be overridden by any value in the inspector for this variable.


    To test of the rigidbody is returning null, you can add this line just after the GetComponent:
    Code (CSharp):
    1. Debug.Log (rb);
    ... And if it's null you'll know.

    Perhaps more elegantly, you could write:
    Code (CSharp):
    1. if (rb != null)
    2. {
    3.      Debug.Log ("Success! We have a Rigidbody reference!");
    4. }
    5. else
    6. {
    7.      Debug.Log ("Failure! We do not have a Rigidbody reference!");
    8. }
    And, yes, make sure you use either the ASDW (WASD) keys, or the arrow keys to move the ball.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Actually, for forces in FixedUpdate, we should not use Time.deltaTime. Primarily this is redundant as FixedUpdate is fixed, but it can also cause issues as forces are optimised in FixedUpdate, and doesn't expect this.
     
  11. mtibo62

    mtibo62

    Joined:
    May 3, 2015
    Posts:
    8
    Yes, none of them are locked
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ok. Humph. Can you describe the exact behaviour again? Or can you so a screen capture of the ball when it's running? I'm still not sure what the error is, but it shouldn't happen.

    The other option is to back up one step and redo all the content of the last video, double checking each step. In this case, we may never know the problem, but you may be able to continue on.
     
  13. Carlos Gherrera

    Carlos Gherrera

    Joined:
    May 7, 2015
    Posts:
    4
    Oh...it was that simple?
    I feel dumb now.
    I was trying to figure out how to calculate the time it took for my pickups to rotate (in seconds) and then figuring out a way to modifying that value.

    What you proposed seems way more efficient and practical.
    Thanks.
     
  14. xanrer

    xanrer

    Joined:
    May 10, 2015
    Posts:
    12
    plz help me i useing lastest version i do everything in video but ball are not moveing :(

    edit:nevermind my bad :D
     
    Last edited: May 10, 2015
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yes, it's pretty simple. If you think about it, Mathf.sin is returning a value based on what we feed it. We are feeding it Time. If we want it to be slower, "slow down" time by multiplying it by a number less than 1.
     
  16. ThaNerdy

    ThaNerdy

    Joined:
    May 10, 2015
    Posts:
    1
    Hey all, quick question.

    When i go to set the canvas for the text, i get a massive wire box that fills the editing scene. Anyway i can get rid of this? Followed the tutorial to the letter, also i didnt get this problem in Unity 4. Not sure if there was a feature added to add the effect or not.

    Settings/anchors are all the same as on the video.

    http://prntscr.com/73tw9g

    When i add new text to the canvas (after resetting the positions under the one of the Cog options) - http://prntscr.com/73tzuc
     
    Last edited: May 11, 2015
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What you are looking at is the canvas. IIRC the scale is 1 unity unit per pixel, so it will be large, and it is aligned to start at origin, so one corner will be in the middle of the scene.

    For more information and details, please see the tutorials on the UI, especially the canvas, in the Learn section.

    You can hide this by hiding the UI layer in the inspector.
     
  18. Freakzilla

    Freakzilla

    Joined:
    Apr 4, 2014
    Posts:
    3
    Perfect! Thanks.

    I've got it all working now. Also added a nice little extra, I think you'll like it ;)

     
  19. Ayo Jmaes

    Ayo Jmaes

    Joined:
    May 11, 2015
    Posts:
    1
    Hi Adam! Im very new to the software and I'm having a major problem with the Roll-A-Ball tutorial. Im pretty sure I'm doing everything OK but if you could just check I'd be grateful!:)
     

    Attached Files:

  20. mtibo62

    mtibo62

    Joined:
    May 3, 2015
    Posts:
    8
    Once again thank you of all the help you are doing!! I will try and capture my game in some sort of way, but until then I will just try and explain the problem. I can start up my game with the ball in it. However, whenever I move the ball with the arrow keys it doesn't roll, only slide and this also makes it very difficult to control the balls movement as well. For, example if I move it around too long it begins to gain momentum and becomes very hard to stop, while in the video it looks as though the control of the ball is very easy to handle. Also, if I move the ball off of the plane it started on and slide it back toward the plane it will hit the side of it and launch off and the begin to spin in the air. The ball never comes down, it only stays in place and slowly rises up. I think there might be some problem with the gravity, but IDK.
     
  21. scott_mcc

    scott_mcc

    Joined:
    May 11, 2015
    Posts:
    5
    Hey guys I am having a problem with Step 3 Moving the player.

    I keep getting the error shown below in a screen shot I took



    I copied the script provided from the tutorial and still appear to be getting this error.
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

    void Start ()
    {
    rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);
    }
    }


    I have tried restarting the project, renaming the script, moving it around yet nothing seems to be allowing it to work. I know I am probably missing something glaringly obvious but after a few hours trying I am hoping you might be able to help.
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    That looks good for the step you're in. The next step is to add the speed variable. If you're worried on a step by step basis, double check the videos during the lesson, and when you're done, you can check against the final scripts posted below the video on the learn page.
     
  23. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Have you checked your settings for things like mass, angular drag, etc? Try resetting the rigidbody component using then context sensitive gear menu. IIRC, the rigidbody uses default settings. The other thing to do is back up a step, and do the part over again, or start from scratch. There's something funky going on, and I can't see where.
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You can check gravity by looking in the physics settings. IIRC project settings / physics.
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please learn to format your code using code tags.
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you talking about the error message on the control (script) in the inspector?

    IIRC all compiler errors need to be fixed.

    What are the errors in your console?

    One thing if note, if the code you posted is supposed to go on that script component, remember that the script name must match the class in the script. That component say "Control" and the code says "PlayerController".
     
  27. scott_mcc

    scott_mcc

    Joined:
    May 11, 2015
    Posts:
    5
    Thanks ! Its working now there were no errors on the console its self but the script name didn't match the class in the script knew it was something simple !

    Thanks a lot saves me pulling the rest of my hair out !
     
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yay!
     
  29. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    Heyas. I was having an issue with this.

    I followed this tutorial as far as Displaying the Score but got stuck at the code editing. I've followed the instructions to the letter but it keeps throwing up odd errors such as
    Assets/_Scripts/PlayerControl.cs(40,27): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
    and
    Assets/_Scripts/PlayerControl.cs(40,25): error CS1547: Keyword `void' cannot be used in this context


    I've attached my code as a .txt file, could someone help me figure out where I went wrong please? Totally new to this T_T
     

    Attached Files:

    • Error.txt
      File size:
      922 bytes
      Views:
      1,011
  30. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You can paste your code directly into the body of the post using code tags.

    In your case you are missing a semicolon.
    Code (CSharp):
    1. SetCountText ()
    ... needs to have a semicolon at the end to finish the statement.
     
  31. mrdarklight

    mrdarklight

    Joined:
    Apr 12, 2015
    Posts:
    2
    Great tutorial. I'm a total beginner in Unity, but I have some experience with scripting, and in Maya 11 (which is actually very similar). Anyway, it covered all the necessary points and it all worked great. Thanks for updating it for Unity 5 (I tried to do it before in Unity 5, before it was updated, and it didn't work).
     
  32. SmittyTwoTimez

    SmittyTwoTimez

    Joined:
    May 15, 2015
    Posts:
    3
    When I roll the ball I go right through the "Pickups" and do not collect them. They do not disappear when I go through it
     
  33. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Glad it worked for you!
     
  34. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This is probably due to something like a tag mismatch, or you've not set up a tag at all.

    If you have the correct code, which will be something like this:
    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.CompareTag ("Pick Up"))
    4.         {
    5.             other.gameObject.SetActive (false);
    6.         }
    7.     }
    If you have a different tag on the pick up objects, like "PickUp", or "pick up", or "pickup" or any other typographical error, then this line:
    Code (CSharp):
    1. if (other.gameObject.tag == "Pick Up")
    ... will return false.

    One of the best ways to make sure it's absolutely correct is to copy the tag from the code and paste it into the tag field in the Tags and Layers panel.

    If you are still having problems, you can try puting in "debug" lines in your code to see if and when the function is called:
    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.         // This will print in the console every time OnTriggerEnter is called
    4.         Debug.Log ("OnTriggerEnter was called!");
    5.         if (other.gameObject.CompareTag ("Pick Up"))
    6.         {
    7.             // This will print in the console if OnTriggerEnter is called and the tag is correct
    8.             Debug.Log ("other.gameObject.tag is correct!");
    9.             other.gameObject.SetActive (false);
    10.         }
    11.         else
    12.         {
    13.             // This will print in the console if OnTriggerEnter is called and the tag is incorrect
    14.             Debug.Log ("other.gameObject.tag is not correct!");
    15.         }
    16.     }
    With this code, you'll have a chance to see what's going on while the game is running.

    There are more robust and complicated ways to "Debug" your game, but this is a good start.
     
  35. Mega_Jimmy

    Mega_Jimmy

    Joined:
    May 12, 2015
    Posts:
    9
    That's sorted it, thanks.
     
  36. SmittyTwoTimez

    SmittyTwoTimez

    Joined:
    May 15, 2015
    Posts:
    3
    I want to make different levels. So once you get the last pickup it brings you to a new world that has more pickup objects. Could anyone help me and or give me the script please and thank you.
     
  37. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Look into the API Application.LoadLevel (check e scripting reference). You may also want to check how to use coroutines to pause a moment before loading the new level, or use a button to advance rather than doing it automatically.
     
  38. SmittyTwoTimez

    SmittyTwoTimez

    Joined:
    May 15, 2015
    Posts:
    3
    I want it to do it automatically. I have no clue what the API is sorry. Can you give me the code and tell me where to put it?
    I am really new and have no clue :)
     
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    No, as a policy, I don't wrote people's scripts, but I will point them in the correct direction.

    The API is the "Application Programming Interface", or as Wikipedia puts it:
    This is the code provided by Unity which we can use to script our games. The Unity API includes all of the functions that we call that are provided by Unity, including "OnTriggerEnter", "Rigidbody.AddForce", "Start", "Update" and more...

    The scripting reference can be found by going to "Help / Scripting Reference" in Unity, or by navigating to the documentation pages on the Unity website.

    There, you can search for "Application.LoadLevel" and see what the documentation pages have to offer.

    Once you've done your research, and if you're still stuck, post the code using code tags, and we can help you from there.
     
  40. sSquared

    sSquared

    Joined:
    May 17, 2015
    Posts:
    1
    I got through the tutorial very well. (it was put together very nice :D) but in the end, when I actually built the game and tested it, none of the text showed up even though it did when i played the game in unity itself.
    latest version of uniy: (5)
    windows 7
    pictures of the settings for the text:
     

    Attached Files:

  41. Slyjoker87

    Slyjoker87

    Joined:
    May 19, 2015
    Posts:
    1
    Hey guys,

    I'm currently working on the Roll-a-Ball game, and I've run into an annoying issue that I'm sure has an easy solution (but I just can't figure it out).

    I'm on the step where we are adding the counter text. In the tutorial video it shows adding the text using Create -> UI -> Text. The text appears along the ground plane. When I try to do the same, my text and canvas are gigantic and perpendicular with the plane. I can go back and set the canvas render mode to world space and resize it manually, but that's really annoying. There has to be a way to get it to align with the ground level, right?

    TYIA
     
  42. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I couldn't quite understand what the issue is. Can you try again? Have you tired to simulate the resolution and aspect ratio in the editor?
     
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Don't be confused by how the Canvas looks in the scene view. The standard Overlay Canvas uses its own hidden camera which you won't see in the scene view, Andre UI won't be rendered by the main camera. The UI is on its own render layer. The Canvas will appear large in your scene. The canvas is scaled to, if I remember correctly, one pixel per one Unity unit. This would make one pixel of your UI about the same size as the player ball or twice the size of one of the pick up objects. If you find your UI distracting in the scene view, you can turn that layer off in the layers pulldown menu in the upper right corner of the editor. When laying out your UI, you should be using the game view to the judge how are UI is being laid out.
     
  44. thelink19

    thelink19

    Joined:
    May 21, 2015
    Posts:
    4
    Hi. I'm a new user of Unity and I have started with Roll a ball. I am stuck at the step 3 because I have this error
    Cattura di schermata (30).png

    I followed all the instructions of the video, but I can't fix the problem

    Cattura di schermata (31).png

    Could you help me? Thanks ;)
     
  45. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ok, a Null Reference means, well the reference is null or empty. It doesn't exist. Note the error message says: "Object reference is not set to an instance of an object" Now, the object reference on line 22 is rb. This means you are not getting the reference to the Rigidbody component successfully.

    Two ways this could happen off the top of my head (there could be more!) are: You don't have a Rigidbody Component attached to your GameObject, so it can't be found with the GetComponent call...

    ... however...

    ... No. 2 is the one that get's the prize: The GetComponent call is not written correctly.

    Looking at your code:
    Code (CSharp):
    1. void start ()
    2. {
    3.     Rigidbody rb = GetComponent <Rigidbody> ();
    4. }
    ... start needs to be Capitalized and be written as:

    Code (CSharp):
    1. void Start ()
    2. {
    3.     Rigidbody rb = GetComponent <Rigidbody> ();
    4. }
    ... otherwise, the automatic call from Unity won't find this function, and it won't get called, unless you call it yourself.

    Don't forget: spelling and capitalization count!

    [Edit] There is one mistake in this. I copied your code and corrected the obvious mistake, without noting the issue with scope and declaring a new variable. See below for my comment.
     
    Last edited: May 21, 2015
  46. thelink19

    thelink19

    Joined:
    May 21, 2015
    Posts:
    4
    I tried changing start in Start, but the error is still there. I think I've attached the Rigidbody component to te object player because there is in the screenshot 1.
     
  47. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ah - missed it. You are recreating a new variable called rb within the scope of Start.

    You have declared:
    Code (CSharp):
    1. private Rigidbody rb;
    ... at the top of the script.

    By using the Type when using rb in Start, you are not just using rb, you are declaring a new variable called rb.

    You should have:
    Code (CSharp):
    1. void Start ()
    2. {
    3.     rb = GetComponent <Rigidbody> ();
    4. }
    You've declared it already, you only need to use it.
     
  48. thelink19

    thelink19

    Joined:
    May 21, 2015
    Posts:
    4
    I use it in this part of code
    Code (CSharp):
    1. rb.AddForce(movement);
    ,
    or not?
     
  49. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This declares a new variable that is a member of the class:
    Code (CSharp):
    1. private Rigidbody rb;
    This declares another new variable (with the same name) that is a member of this function only:
    Code (CSharp):
    1. void Start ()
    2. {
    3.     Rigidbody rb = GetComponent <Rigidbody> ();
    4. }
    This new variable will not be accessible to the rest of your script, as its scope is within the Start function.

    This will also not set the variable that was declared in the body of the class (at the top of the script). It is a different variable.

    The following code will simply use the variable declared in the body of the class (at the top of the script), but not create a new onw. It is the same variable declared in the body of the class:
    Code (CSharp):
    1. void Start ()
    2. {
    3.     rb = GetComponent <Rigidbody> ();
    4. }
    This will set the variable declared in the body of the class (at the top of the script) as it is the same variable.

    Use this last piece of code.

    Using the Type declares a new variable:
    Code (csharp):
    1. Rigidbody rb = ...
    With only the name, and not the Type, the variable is being used, but must be declared before it is used:
    Code (csharp):
    1. rb = ...
     
  50. Deleted User

    Deleted User

    Guest