Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. 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. Ghost_Tech

    Ghost_Tech

    Joined:
    Oct 20, 2015
    Posts:
    7
    Hi all,

    Just finished the video regarding scoring and text displaying.

    I can make the text display for the count.

    However I cannot make the You Win text appear.

    Below is my code.

    As far as I am aware I have linked the text object to the player object.

    Im completely stuck here.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public Text countText;
    9.     public Text winText;
    10.    
    11.     private Rigidbody rb;
    12.     private int count;
    13.  
    14.        
    15.     void Start ()
    16.     {
    17.         // Link the script to the rigidbody attached to the game object
    18.         count = 0;
    19.         SetCountText ();
    20.         rb = GetComponent<Rigidbody>();
    21.         winText.text = "";
    22.        
    23.     }
    24.  
    25.     // FixedUpdate is called before performing any physics calculations
    26.    
    27.     void FixedUpdate ()
    28.     {
    29.         float moveHorizontal = Input.GetAxis ("Horizontal");
    30.         float moveVertical = Input.GetAxis ("Vertical");
    31.        
    32.         //Create a variable to place the above 2 values into a Vector3
    33.        
    34.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    35.        
    36.         rb.AddForce (movement * speed);      
    37.     }
    38.    
    39.     void OnTriggerEnter(Collider other)
    40.     {
    41.         if (other.gameObject.CompareTag("Pickup"))
    42.         {
    43.             other.gameObject.SetActive (false);
    44.             count = count + 1;
    45.             SetCountText ();                      
    46.         }
    47.     }
    48.    
    49.     void SetCountText ()
    50.     {
    51.         countText.text = "Count: " + count.ToString ();
    52.         if (count >= 13)
    53.         {
    54.             winText.text = "You Win";
    55.         }
    56.     }
    57. }
     
  2. Ghost_Tech

    Ghost_Tech

    Joined:
    Oct 20, 2015
    Posts:
    7
    On another note, when building I do not have an option to build to the windows platform, only Mac and Linux are available.

    Not sure if this is because I am using Windows 10.
     
  3. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836

    Hi,

    if your scoring is working, ie the counting then have a look at your SetCountText() method.
    can you double check that you have 13 pickups in your scene.
    as this will check to see if count is 'greater or equal' to 13, but if theres only 12 in the scene like the video, it will never reach there.

    change that to
    Code (CSharp):
    1. if (count >= 5)
    to test.

    that way once you pick up 5 or more, the win text should appear, then you can put it back to amount of pickups you have in the scene.
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    First thing to check, do you have 13 or more items? In the lesson we have 12. The code is using a fixed coded value, and you've set the to 13. If you can't collect 13 or more items, you don't win.

    One other option is to make a public int winCount; and set this in the inspector to the number of collectables in the scene. Then change the code testing for win to if (count >= winCount)... This way you now have control over the number of items needed to win from the inspector.

    Another option is to make winCount into a private variable and in the Start() function set the countValue to equal FindGameObjectsWithTag("Pick Up").Length; For this to work, you will need to make sure the tag on the collectables prefab is set to "Pick Up", or whatever the tag value is in the lesson. This way, all the tagged objects will be put into an array, and you'll use the length of the array, or the number of counted pick up objects, as your final target.
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Woah! Cross post! Thanks oboshape!
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I can't speak to Windows 10 support, But I'm sure that a good pair of google goggles will answer that.
     
    OboShape likes this.
  7. bughuntr

    bughuntr

    Joined:
    Oct 27, 2015
    Posts:
    1
    So I'm a bit lost here in this beginner tutorial as I don't know where I get Assignment 1 from. Should this have been loaded in some getting started folder or location that is not explained in the video?
    Thanks!
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I have changed the layout on the page. When the original project was recorded, the tutorials were all in one set. The introduction was an invisible episode 0. The rest, starting at 1, were in order.

    Now they have been re-organized into subgroups.

    I've changed the layout of the page to be a little more clear.

    92dddffc5894335a67f9bda4dec06a8e.png

    I can't change the numbers at the top of each episode, as these are automatic, but perhaps by setting "Introduction" into its own section, then Section 1, Episode 1 will make more sense.
     
  9. Natasa

    Natasa

    Joined:
    May 28, 2013
    Posts:
    14
    I saw this tutorial and I wanted to create my own game. I wanted my ball rolling through labyrinth using device's accelerometer. But my ball doesn't roll as it should. When I tilt my device to left, the ball still rolling to the right and it's almost impossible to roll backwards. Can someone give me any advice, please?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public float force;
    8.     private Rigidbody myRigidBody;
    9.        
    10.     void Start()
    11.     {
    12.         myRigidBody = gameObject.GetComponent<Rigidbody> ();
    13.     }
    14.  
    15.     void FixedUpdate()
    16.     {
    17.         Vector3 move= new Vector3 (Input.acceleration.x, 0.0f, -Input.acceleration.z);
    18.         myRigidBody.AddForce (move* speed * Time.deltaTime);
    19.     }  
    20. }
    21.  
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    One thing, minor, I don't believe you need Time.deltaTime with AddForce in FixedUpdate.

    Can you display the current acceleration?

    You can write a little script that displays the Input.acceleration to the screen, and see if you are getting the forces you expect.
     
  11. Natasa

    Natasa

    Joined:
    May 28, 2013
    Posts:
    14
    If I delete Time.deltaTime in FixedUpdate then the ball is much faster. I decreased the speed but movement stays the same.
    If I display current acceleration:
    - for Input.acceleration.x I get negative values if I tilt device left and positive values on right
    -
    for -Input.acceleration.z I get negative values if I tilt device forward and positive on backwards

    I assume something's wrong with myRigidBody.AddForce...
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are the values you are getting consistent? Are you getting equal values for x and z depending on tilt in each direction?

    Also, realize that for this to work unmodified, you will need to have the device parallel to the floor, and the coordinates are in real world space.
     
  13. James Pulliam

    James Pulliam

    Joined:
    Jan 2, 2013
    Posts:
    1
    the tutorials are duplicated. the first video of the lessons is the same as the 4th video. setting up the play area.
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Whooops. I was having some issues. Let me check.
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ah - there was "Setting up the Play Area" twice, not "Setting up the Game" and "Setting up the Game Area".

    Fixed. Thanks!
     
  16. raveenbouy

    raveenbouy

    Joined:
    Oct 30, 2015
    Posts:
    2
    i got this error =/ pls help. "
    Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported in Unity 5.
    If you want to use a non-convex mesh either make the Rigidbody kinematic or remove the Rigidbody component. Scene hierarchy path "Plane", Mesh asset path "" Mesh name "Plane" "
    What's happening there is that when i play it, the plane and the object falls to bottom which means Position Y value Decreases =/ how do i fix it?
     
  17. raveenbouy

    raveenbouy

    Joined:
    Oct 30, 2015
    Posts:
    2
    nvm fixed it ^_^ ty but the ball doesn't move =/


    Update : fixed it ^_^ there was a error in my code
     
    Last edited: Oct 30, 2015
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  19. jmoriarty

    jmoriarty

    Joined:
    Oct 13, 2015
    Posts:
    1
    I am working on tutorial 7 "Displaying Score and Text" in the Roll a Ball tutorial. The NullReference Exception error is as follows: Object reference not set to an instance of an object PlayerControl.Start() (at Assets/Scripts/PlayerCotrol.cd: 21.

    Line 21 has the following code winText.text = ""

    I re-created all of my objects and still get the same error message. The Player ball rolls and the Pick Ups spin, but the counter does not work.

    Here is the PlayerControl code:
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class PlayerControl : MonoBehaviour {

    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;


    void Start ()
    {

    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText ();
    winText.text = "";
    }

    void FixedUpdate()

    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVerticle = Input.GetAxis ("Vertical");

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

    rb.AddForce (movement * speed);


    }

    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag ("Pick Up"))

    {
    other.gameObject.SetActive (false);
    count = count + 1;
    SetCountText ();
    }
    }
    void SetCountText ()
    {
    countText.text = "Count: " + count.ToString ();
    if (count >= 8)
    {
    winText.text = "You Win!";
    }
    }
    }
     

    Attached Files:

  20. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Can you please use code tags when the posting code on this forum?
     
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you are using text not GUI text, have you used the ui name space?

    Have you associated the game object with the text element with the public field in the inspector?
     
  22. l889as

    l889as

    Joined:
    Nov 2, 2015
    Posts:
    3
    HI, I got some problem with this example. I followed every step in the video but the ball didn't work.
    I even copied all of your code but nothing change. Can u help me? Thanks a lot. 1.png
    2.png
     
  23. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What do you mean by "doesn't work"?
     
  24. l889as

    l889as

    Joined:
    Nov 2, 2015
    Posts:
    3
    I followed every step. However,when I clicked the "play button",nothing happen. The ball was still there without moving.
    And I didn't see any error message. Is it possible that my computer is not powerful enough? OR some problem with Chinese system(Windows 8.1) which I didn't notify. Thanks for your replying..
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you using the asdw keys or the arrow keys to move?

    Can you give me any more information about what you see doing and what you've tried? Have you saved your script? Have you restarted unity and / or your machine?
     
  26. l889as

    l889as

    Joined:
    Nov 2, 2015
    Posts:
    3
    HI, I have found the problem. I made some misunderstanding. Now, everything goes alright. Thanks a lot.
     
  27. Mr_Botz

    Mr_Botz

    Joined:
    Nov 5, 2015
    Posts:
    2
    Thank you so much, I am really beginner on Unity and it was good way to start ^^
    I would like to see if you could add a menu into this game or some buttons to exit-restart game when it is over.
     
  28. orkney

    orkney

    Joined:
    Nov 5, 2015
    Posts:
    7
    Would just like to say thanks to whoever put together the video tutorials :) It really has been a great introduction to Unity and I'm excited to learn more.
     
  29. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You're welcome! Thanks for the feedback! It's always appreciated.
     
  30. Cookiez

    Cookiez

    Joined:
    Nov 1, 2015
    Posts:
    13
    Hello, I'm doing this tutorial at the very moment but one thing is troubling my mind. I'm using Visual Studio for the code writing but in some parts of this tutorial you say to shiftmark the word and click ctrl+' to open the documentation. However my Visual Studio isn't anyhow connected to any sort of documentation and as long as I have a video tutorial it's all fine but if I want to make sth in the future that kind of shortcut would be very useful. Could you please tell me how to link that site to ctrl+' step-by-step?
     
  31. orkney

    orkney

    Joined:
    Nov 5, 2015
    Posts:
    7
    I had the same problem. If you highlight the word in Visual Studio you can then use "Help" on the main menu and select the link "Unity API Reference" and it will take you to that page :)
     
    Last edited: Nov 9, 2015
    Cookiez likes this.
  32. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, humph. Not sure what to say. Monodevelop is the official
    Unfortunately, I can't really help with this, as MonoDevelop is the officially supported editor for both Mac OS and Windows. We do include VS on Windows, and there is a closer relationship, but as we don't (afaik) support VS beyond the most basic hooks, I can't really make a good suggestion. Also, it's worth keeping in mind that as we create our official tutorials, we try to use our official material, so we will most likely continue to use MonoDevelop in our training material until something changes with our relationship with other script editors.
     
    Cookiez likes this.
  33. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Thanks for the suggestion! I am sure many people will benefit from this.

    I do know that there are hooks for making the "go to line in script" functionality when clicking on the console - and people have set this up for other IDE's like SublimeText, and I presume this functionality is available to VS (and it may even be implemented). Perhaps the functionality of adding a "hot-key" to VS is do-able? Unfortunately, I don't have VS installed on my machine, and I use MonoDevelop for my official tutorials - and I use a completely different (in-Unity-editor) scripting solution for my personal projects.
     
  34. Damianexxx

    Damianexxx

    Joined:
    Mar 15, 2015
    Posts:
    5
    Hi. I have problem with compilation.
    I think its library fault. I cant move a ball.
     

    Attached Files:

    • 123.png
      123.png
      File size:
      97.8 KB
      Views:
      864
  35. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Have a look through this post,
    http://forum.unity3d.com/threads/visual-studio-code-integration.322440/

    there is a mention of that error in there.

    does it compile if you switch to MonoDevelop?

    Does it give you a better description in the console window within unity at all?
     
    Last edited: Nov 10, 2015
  36. Damianexxx

    Damianexxx

    Joined:
    Mar 15, 2015
    Posts:
    5
    i install that visual code editor and code is good but i idk why this not compile.
    Where i can find "monodevelop"?
     
  37. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    in Unity if you go to Edit > Preferences.
    External Tools.

    theres a dropdown there where you can select the External script editor you want to use.
    from there you can select monoDevelop that comes bundled with Unity.

    you can always switch back (just take a note of what you have selected there already, probably Visual Studio 2015)

    see how that goes. if that works fine, then its something with VS that I personally cant help with im afraid as I dont use it for Unity at all.
     
  38. Cookiez

    Cookiez

    Joined:
    Nov 1, 2015
    Posts:
    13
    Thanks for the help, the documentation shows up in the Visual Studio.

    On the other hand I have an idea of extending our game. I'd like to have a script that would respawn the cubes in random places everytime a ball collides with one that is already on the ground. That means that the game would never end but I'm more ok with that version than with the one that ends it after scoring 12 points.
    Can anyone write such a script or at least give me hints where I should look for some educational materials about C# for Unity?
     
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Look up Instantiate in the docs. Look at the Random class in the docs and work out how to instantiate a prefab at a random location. This is covered in space shooter if you want to move on to another project.
     
  40. demeter52

    demeter52

    Joined:
    Nov 14, 2015
    Posts:
    1
    Hi!
    I guess it's totally unnecessary to compare the tag of the collider to "Pick up" in the onTriggerEnter method because the only triggers are the cubes. I tested without the comparing and it wordked fine!
    Another thing I'd like to mention is that it's better to Destroy the cubes when collision because we can release memory and it can count much on Phones.
     
  41. BboyIbsta

    BboyIbsta

    Joined:
    Nov 14, 2015
    Posts:
    1
    Sir, I wanted to ask, why is my canvas showing up like this? It shows up in such a way that my level is in one corner of it? I cant even edit the rect transform of the Canvas. What should i do?
    sir2.PNG
     
  42. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening,

    the Canvas object, in our case you can imagine as a screen overlay, it covers the entire area of the screen in our game view.
    this Rect transform on the Canvas in this case cannot be changed as it is sized automatically based on the Game view window size.

    The Canvas is super massive in comparison to the game objects in our game, but this is normal ( i think its scale is roughly 1 unity unit per screen pixel, or something like that, to give it better fidelity)

    for info and a much better explanation of the canvas and the UI, have a look at Adams UI Lessons.
    http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-canvas?playlist=17111

    To continue with the roll a ball tutorial from where you are now, if you change over to your game view tab, select your Text object that you have under your Canvas you should be good to carry on. and as you will see the Rect Transform of your Text object is editable like in the video.

    http://unity3d.com/learn/tutorials/projects/roll-a-ball/displaying-text?playlist=17141
     
  43. ZombieChop

    ZombieChop

    Joined:
    Nov 15, 2015
    Posts:
    3
    Hello, I am on part 3.2 in the tutorial and I'm stuck. Can anyone tell me what i am doing wrong?
    Thanks!
    error1.PNG playercontroller.PNG
     
  44. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @ZombieChop,

    if you have a look at your inspector as you have there, it is looking for a script called 'playerControls' which doesnt exist.

    you can either use the context gear menu to remove the playerControls script component, and then add your playerController script.

    or press the little circle select button to the right of the script component on your player and select the playerController script from the box that pops up so that it is looking and using the correct script.

    hope that helps.
     
  45. ZombieChop

    ZombieChop

    Joined:
    Nov 15, 2015
    Posts:
    3
    Thank you @OboShape I really appreciate your help!

    That took care of that error but now i am getting this error.

    "There are inconsistent line endings in the 'Assets/scripts/PlayerController.cs' script. Some are Mac OS X (UNIX) and some are Windows. This might lead to incorrect line numbers in stacktraces and compiler errors. Many text editors can fix this using Convert Line Endings menu commands."

    Thank you!

    upload_2015-11-15_15-56-7.png
     
  46. ZombieChop

    ZombieChop

    Joined:
    Nov 15, 2015
    Posts:
    3
    i just restarted unity and the error was gone, and it is working now, Thank you for the help @OboShape !
     
    OboShape likes this.
  47. W1N5T0Nz

    W1N5T0Nz

    Joined:
    Nov 7, 2015
    Posts:
    3
    Hello There!

    I am trying to teach myself Unity and I though that it would be a good idea to start off with the roll-a-ball tutorial. I got as far as lesson 3. I have not been able to get the player controller script to work. The player will not move regardless of any keys that I press. I started with just blank Unity project with no assets imported. If there is any asset that I has to be imported to allow keyboard input I have not added that. As I have noted before I am a complete beginner at Unity and I am sorry for bothering all of you with my probably stupid questions. I have tried many different things from the forums and various different places on the internet. This is my code at this point and I am getting a few errors that I hope someone can help me with.

    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour {
    2.     private Rigidbody rb;
    3.     void Start ()
    4.     {
    5.         rb = GetComponent<Rigidbody>();
    6.     }
    7. void Update ()
    8.     {
    9.         float moveHorizontal = Input.GetAxis("Horizontal");
    10.         float moveVertical = Input.GetAxis("vertical");
    11.    
    12.         Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
    13.      
    14.         rb.AddForce(movement * speed);
    15.      
    16.     }
    • error CS0116: Anamespace can only contain types and namespace declarations
    • Also, I am getting an error saying that there are no MonoBehavior scripts in the folder. It shoud be noted that I am making this game on Windows. So I am using Visual Studio for all of my script editing. I don't know if this makes a difference or not so I just wanted to make that known.
    • I am also getting a parsing error or two (I dont know the error codes, if that is needed let me know. It probably is so I will post them as soon as possible.
    Let me know if you need anymore information (besides the parsing error codes). Thank you in advance for any help that is given.

    PS: I know that MonoDevelop is the official script editor used in Unity. I have been attempting to change MonoDevelop to the default script editor but even when it is saved as the default in the preferences menu Visual Studio will still open. Any help with that would also be appreciated.
     
  48. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @W1N5T0Nz,

    Good call in starting with the RollaBall tute :)

    a couple of things that I can see that might be possible gotchas.

    If in your inspector in unity you are getting an error saying that there is no script or script is missing.
    Either you have deleted it or renamed it and not reflected the change in name within the script.

    for example :
    where you have the line
    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour {
    the class name must be the same as the filename of the script.
    so if your filename was PlayerController, then this line should read
    Code (CSharp):
    1. public class PlayerController: MonoBehaviour {
    so double check that please.


    not sure if its how you have copied and pasted, but there appears to be a missing ' } ' at the end of the script, to end the block that encompasses the class.
     
  49. W1N5T0Nz

    W1N5T0Nz

    Joined:
    Nov 7, 2015
    Posts:
    3
    Hello Again,
    I am still having issues with this little thing.
    MonoBehavior.PNG
    I have no idea what to do. I am no longer having any parsing errors. When I run the game I only get two warnings and the player sphere will not move regardless of anything that I press. The two errors that I get now are these.
    MissingReference.PNG
    I am pretty sure that these are related to the previous message.
    This is the script that I am using right now.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.  
    7.  
    8.     void fixedUpdate()
    9.     {
    10.  
    11.         float moveHorizontal = Input.GetAxis("Horizontal");
    12.         float moveVertical = Input.GetAxis("Vertical");
    13.  
    14.         var speed = 1;
    15.  
    16.         Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
    17.  
    18.         GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    19.     }
    20.  
    21.  
    22. }
    The missing bracket at the end was just a copying and pasting error on my part. I hope that you can help me with this.
    Thanks Again In Advance,
    W1N5T0Nz
     
  50. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Arguably, yes... IIRC, in the context of the video, we start of discussing OnCollision and then OnTrigger. Learning what tags do is also important, as I feel only very simple games like our learning examples, will only have one type of thing with triggers on them.