Search Unity

Official Roll-a-ball Tutorial Q&A

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

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    A null reference exception means that a referenced object is missing or null. Check line 20 in your plater controller script. This is where the issue in the code is, indicated by the error.

    Often this means you have not populated a public variable, or a GetComponent call fails, usually due to a typo or capitalisation error.
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Double check that the tag has been created and assigned. There is an anomaly where the tags and layers panel dies not have the tag correctly saved. Once you have verified the tag in the tags and layers panel, reassign the tag to the prefab to be sure.
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    First off, look into platform dependent compilation for a better way to deal with cross-platform code.
    http://docs.unity3d.com/Manual/PlatformDependentCompilation.html

    I'm unclear what Could be causing these graphics issues. Try looking at your render path,mane perhaps post in the Android section for more help. Reference my account I'd or post here if you discover the issue.
     
    john1122 likes this.
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I am on holiday until the 10th of September with little or no access to the Internet, so expect delays in my responses.
     
    IanSmellis likes this.
  5. saadmanf

    saadmanf

    Joined:
    Aug 14, 2015
    Posts:
    2
    Hi !! This tutorial has been a great help, thank you. I wanted to know how i can build this gor android just to test !! Can anyone help ??
    TIA
     
  6. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    IanSmellis and saadmanf like this.
  7. saadmanf

    saadmanf

    Joined:
    Aug 14, 2015
    Posts:
    2
  8. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    Hi there Adam!! ^^
    First of all i wanted to Really thank you for all the tutorials!! :'3 ... Really :'3
    But apart of that we both know that i came here for another thing apart of giving you the "Thanks" xP
    Now, i want to make it clear (Because i already have the same problem with a guy on Unity Answers ...)
    ..... I do not want the Script ... I want the logic =) (y)
    I have been working on a entirely new game based (On part) on you Roll a Ball project, now, my new project uses the Android Accelerometer device to control the ball ... thing that i already implemented.
    But, now i have the problem that the game initialize with the device (my tablet) on a horizontal orientation with the screen looking at the "sky" so i came here hoping for you to share some of your knowledge on "Programming Logic" to tell me what to do for initialize the game with the initial orientation that the user is holding it.
    The code for you to have an idea:
    A little "Resume" ... i want te game to save the initial orientation and value of the accelerometer respect to the user's "face".

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public GameObject DeathParticles;
    8.  
    9.     private Rigidbody rb;
    10.     private Vector3 spawn;
    11.  
    12.  
    13.     void Start ()
    14.     {
    15.         rb = GetComponent<Rigidbody>();
    16.         spawn = transform.position;
    17.     }
    18.  
    19.     void Main ()
    20.     {
    21.         Screen.sleepTimeout = SleepTimeout.NeverSleep;
    22.     }
    23.  
    24.     void OnCollisionEnter (Collision other) {
    25.         if (other.gameObject.tag == "Enemy")
    26.         {
    27.             Die();
    28.         }
    29.  
    30.     }
    31.  
    32.     void Die(){
    33.         Instantiate(DeathParticles, transform.position, Quaternion.Euler(270,0,0));
    34.         transform.position = spawn;
    35.     }
    36.  
    37.     void Update(){
    38.         if (transform.position.y < -2)
    39.         {
    40.             Die();
    41.         }
    42.     }
    43.  
    44.     void FixedUpdate ()
    45.     {
    46.         if (SystemInfo.deviceType == DeviceType.Desktop)
    47.         {
    48.             float moveHorizontal = Input.GetAxis("Horizontal");
    49.             float moveVertical = Input.GetAxis("Vertical");
    50.  
    51.             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    52.  
    53.             rb.AddForce (movement * speed);
    54.         }
    55.         else
    56.         {
    57.             float moveHorizontal = Input.acceleration.x;
    58.             float moveVertical = Input.acceleration.y;
    59.        
    60.             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    61.        
    62.             rb.AddForce (movement * speed);
    63.         }
    64.     }
    65. }
     
  9. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon,
    I'm not really too familiar with the mobile devices, but thought i would chip in a little thought.
    In Mikes live session he mentions device orientation (at roughly 40 mins in)
    http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/mobile-development

    where he mentions that you can only practically use two of the Accelerometers axis, as in either of the two orientations, screen facing up, of screen facing towards you. as in each scenario one of the axis will have gravity applying a solid -1 value. which would give an indication of how your device is orientated.
    Then possibly based on that given two orientation cases, you can then alter which axis to use for movement.

    might be worth a look at mikes lesson to give you a few ideas while waiting for Adam to advise further.

    ObO
     
    Maenor likes this.
  10. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    Oh, i din't about that! I'll take a look at it! Thanks!
     
  11. rybicki.richard

    rybicki.richard

    Joined:
    Jul 21, 2015
    Posts:
    6
    Hello Mr. Buckner,

    I am currently having this problem on Roll-A-Ball during "moving the player" video. I have wrote the script to move my little ball but when I try to test the game and move him I get an alert "ArgumentException: Input Axis horizontal is not setup."

    I have gone to where you are supposed to set up the controls and everything seems to be right but I keep getting this argument. please help my little ball move! thank you!

    v/r
    Richard Rybicki
     
  12. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    mmm ... if you already checked the Input configuration on Project Settings < Input and everything seems to be right, then, you should post the script so we can help you with you problem ...
    Also ... Things sometimes appears to be OK but that may no be always the case ... If you can, upload a pic of your input settings on Unity.

    Adam is on vacations! xP We must help one at another ;)
     
  13. SpaceOverflow

    SpaceOverflow

    Joined:
    Aug 17, 2015
    Posts:
    2
    Hi. Thanks for the tutorial. In step 3.2, I am having a problem with the ball disappearing and not the cubes. Everything looks right and I am baffled as to what the problem is. Thanks.
     

    Attached Files:

  14. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    Your code seems to be right, check the Tags with more detail or upload a Picture! =)
    Btw ... You don't have to upload all the project xP Only the scripts or a single capture it's enough.
     
  15. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    I found your problem!! =)
    After all It is the script xP
    On your player controller Script you must specify a single work on the OnTriggerEnter function! =)
    Your version:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.         if (other.gameObject.CompareTag ("Pick Up")) {
    3.             gameObject.SetActive(false);
    4.         }
    How it should be:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.         if (other.gameObject.CompareTag ("Pick Up")) {
    3.             other.gameObject.SetActive(false);
    4.         }
    Notice the difference? =)
    The word "other." xP
     
  16. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Good Morning,
    for your input.GetAxis, this should be "Horizontal" with a capital H.
     
  17. travis272

    travis272

    Joined:
    Aug 17, 2015
    Posts:
    3
    hi I have an issue of the player not moving I have a error of :
    NullReferenceException: Object reference not set to an instance of an object
    playercontroller.FixedUpdate () (at Assets/scripts/playercontroller.cs:22)

    I have gone through the tutorial 5 times and I cant see anything wrong.
    this is my coding:

    using UnityEngine;
    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 moveVertival = Input.GetAxis ("Vertical");

    Vector3 movment = new Vector3 (moveHorizontal, 0.0f, moveVertival);

    rb.AddForce (movment * speed);
    }
    }
     
  18. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Morning @travis272,

    A null reference exception indicates that you are trying to use something that has not been set.
    in your case line 22 refers to your attempt to use the rb Rigidbody reference.

    in your script this has not been set as the Start() function is incorrect and therefore your rb reference has not been set up.
    if you change your Start function to have a capital S like below, it should be ok as it wont thow an error, it just wont run it.
    Code (CSharp):
    1.    void Start ()
    2.        {
    3.              rb = GetComponent<Rigidbody>();
    4.        }
    as a quick addition, when including code within a post, its sooo much easier to read if you can use code tags in the future ;)
     
    Last edited: Aug 17, 2015
  19. travis272

    travis272

    Joined:
    Aug 17, 2015
    Posts:
    3
    thanks works grate now
     
  20. SpaceOverflow

    SpaceOverflow

    Joined:
    Aug 17, 2015
    Posts:
    2
    Many Thanks Maenor!! That worked. Much appreciated. It must have come with the copying and pasting. I missed that other part for some reason.

    Thanks again!
     
    OboShape and Maenor like this.
  21. qqoopp0

    qqoopp0

    Joined:
    Aug 17, 2015
    Posts:
    7
    Hello friends,

    I am now at 6 out of 8. Not sure when but now my Player, the ball, will move toward one direction when I started the"Play mode". Cannot spot what's the problem yet. My position of the ground is 0,0,0 and the position of the player is 0,0.5,0 and the speed is 10. The higher the speed, the higher the dragging force toward that direction.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed; //make it editable outside the script
    7.    
    8.     private Rigidbody rb; //the Rigidbody for Physics
    9.    
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody>(); //get the initial condition
    13.     }
    14.    
    15.     void FixedUpdate()
    16.     {
    17.         float moveHorizontal = Input.GetAxis("Horizontal"); //get the coordinate
    18.         float moveVertical = Input.GetAxis("Vertical"); //get the coordinate
    19.        
    20.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    21.         //x, no ups and downs, y
    22.        
    23.         rb.AddForce(movement * speed);
    24.         // add force to that particular coordinate
    25.     }
    26.  
    27.     //
    28.     void OnTriggerEnter(Collider other)
    29.     {
    30.         //sample code to destory the object: Destory(other.gameObject);
    31.         if (other.gameObject.CompareTag("Pick Up"))
    32.         {
    33.             other.gameObject.SetActive (false);
    34.             // equal to the check box Activate/ Deactivate the object
    35.         }
    36.     }
    37.  
    38. }
    39.  
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
     
  22. rybicki.richard

    rybicki.richard

    Joined:
    Jul 21, 2015
    Posts:
    6
    Maenor attached is a copy of my script
     

    Attached Files:

  23. rybicki.richard

    rybicki.richard

    Joined:
    Jul 21, 2015
    Posts:
    6
    I got the copy to work. sorry I'm new to this


    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    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);
    }
    }
     
  24. rybicki.richard

    rybicki.richard

    Joined:
    Jul 21, 2015
    Posts:
    6
    OboShape, Capitalizing the H worked! now my next problem is that I am getting "NullReferenceException: Object reference not set to an instance of an object"
     
  25. qqoopp0

    qqoopp0

    Joined:
    Aug 17, 2015
    Posts:
    7
    I restarted a whole new project. The ball indeed moves automatically to one direction at the beginning already.
    I cannot find the reason but it is related to the script
    Code (CSharp):
    1. rbball.AddForce(movement);
    After I hid it, the ball is stable even I set it's position at high ground, it will fall straight from the sky.
    But with that script in action, the ball will fall with a direction......
    how come :(

     
  26. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Ah now that you have posted your code up, there's a couple of gotchas in there to look out for.

    firstly your Start function should have a Capital S like below
    Code (CSharp):
    1. void Start ()
    2. {
    3. rb = GetComponent<Rigidbody>();
    4. }
    as if you use a lower case s, this function will not get called, and therefore will not initialise your rb variable and it will be null (hence one of your null reference errors)

    and within your FixedUpdate both of the input axis should be capitalised within the quotes
    Code (CSharp):
    1. void FixedUpdate ()
    2. {
    3. float movehorizontal = Input.GetAxis ("Horizontal");
    4. float movevertical = Input.GetAxis ("Vertical");
    try that and see how you get on.

    Oh, and if you can, use code tags when posting code, just makes it alot easier to read ;)
     
  27. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    can you post the rest of your code from your PlayerController script please, just for a look, sounds like there could be something funky with the movement vector. that is if, like you say, it only has this movement applied when the script is active.
     
  28. rybicki.richard

    rybicki.richard

    Joined:
    Jul 21, 2015
    Posts:
    6
    Great I got my ball moving thank you! and how do I do code tags. Thanks!
     
  29. qqoopp0

    qqoopp0

    Joined:
    Aug 17, 2015
    Posts:
    7
    Dear Friend,

    I posted the whole script a few post upward at #271.
     
  30. DopiestScripts

    DopiestScripts

    Joined:
    Aug 20, 2015
    Posts:
    1
    I have a problem......
    My Player wont Pick Up the objects :(
    I dont tinnk there is a problem with my codeing.

    void OnTriggerEter(Collider other)
    {
    if (other.gameObject.CompareTag("Pick Up"))
    {
    other.gameObject.SetActive (false);
    }
    }

    And the tag name is the same as in the codeing but it still dose not work, and i cant find the problem!! PLZ HELP?
     

    Attached Files:

    Maenor likes this.
  31. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    Well there is a problem with the Code xP ... Just look at the first function! ;) Pay attention to its name!! =)
     
    IanSmellis likes this.
  32. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Sorry, never noticed.
    And apologies for the late reply, arrived offshore with no WiFi for a few days.
    Not sure, is the balls rotation zeroed, might be using a local coords. Sorry mate not sure. I've only got access to my phone for a week :(
     
  33. cashcogaming

    cashcogaming

    Joined:
    Aug 20, 2015
    Posts:
    1
    I am on the pickup objects stage. Everything was working fine. I ran into this really wierd situation where instead of the pickup objects being deactivated and "picked up" on collision, my ball is being picked up by the pickup objects. iIve gone through the video over and over and have no clue what I have messed up. Any advice would be greatly appreciated.
     
    Maenor likes this.
  34. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    You should post you Script to see if there is any errors!! ^^
     
  35. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Sounds like something you picked up on maenor.

    @cashcogaming, have a look at your OnTriggerEnter function.
    Just to make sure you are setting the other.gameObject inactive.
    I cant post a code example as I'm on my phone.
     
  36. sweetamber

    sweetamber

    Joined:
    Aug 21, 2015
    Posts:
    2
    Do all the videos work with unity 5.1.2
    I got an error when i started scripting in the part 3 of the video.
    It said something about not using Void with the fixedupdate part of the script.
    Should i get an older version of unity in order to make use of these videos or?..
    My unity program looks different than yours, what version are you using in the videos ?..
     
    Maenor likes this.
  37. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    Check your uppercases and lowercases on those sentences! =)
    Unity version doesn't have anything to do with it! ^^
    If you can't find the solution post the code and we will help you!! (y)
     
    sweetamber and IanSmellis like this.
  38. sweetamber

    sweetamber

    Joined:
    Aug 21, 2015
    Posts:
    2
    So i fixed the code, watching all the related videos posted below the tutorial, helped me understand the scripting a little better, however.. now the ball just won't move, there is something that is not right, the script is right, but i get an error everytime i enter playmode and press the keys saying..
    ''The referenced script on this behavior is missing''

    seems like an easy fix, i just can't seem to find it.. am i an idiot :D ? Unity'.png
     
  39. danbannana

    danbannana

    Joined:
    Aug 24, 2015
    Posts:
    4
    I've got a problem. When I press play the ball won't roll. it says that the referenced script on this behaviour is missing. the script is the player_controller script. this is my code

    usingUnityEngine;
    usingSystem.Collections;

    publicclassPlayerController : MonoBehaviour {

    publicfloatspeed;

    privateRigidbodyrb;

    voidStart ()
    {
    rb = GetComponent<Rigidbody>();
    }

    voidFixedUpdate ()
    {
    floatmoveHorizontal = Input.GetAxis ("Horizontal");
    floatmoveVertical = Input.GetAxis ("Vertical");

    Vector3movement = newVector3 (moveHorizontal, 0.0f, moveVertical);

    rb.AddForce (movement * speed);
    }

    voidOnTriggerEnter(Colliderother)
    {
    if (other.gameObject.CompareTag ("Pick Up"))
    {
    other.gameObject.SetActive (false);
    }
    }
    }


    please tell me what is wrong. and before this was happening when i rolled my ball into the boxes my ball went straight through!! :mad::mad::mad::mad::mad:
    thanks.
     
  40. TrebleSketch

    TrebleSketch

    Joined:
    Aug 22, 2015
    Posts:
    5
    Hello, this tutorial has been of great help.
    But in the 7th video, with the "count.ToString" command. Unity 5 says that it can't find the command or something :|
     
  41. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    I don't think it has anything to do with the content of the script. This errors occurs when you have a script attached to a gameobject and you deleted, moved or renamed it outside of unity. Unity can't find the related script for the MonoBehaviour component.

    If you want to rename or move a script do it always in Unity. To solve your error, just find the component that references this missing script and drag the right script onto the component. (Doble Click on the error)
    Let me know of the result!! ^^
     
    OboShape likes this.
  42. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    @danbannana It should be the same problem as @sweetamber ^ (My Previous message)
    Let me know if you can solve it! ^^
     
    OboShape likes this.
  43. Maenor

    Maenor

    Joined:
    Aug 6, 2015
    Posts:
    14
    Sorry but i need you to be a little more specific!! u.u Post the code (Using the Tools on this same "Message-Box" kinda thing) And i can help you further! =)
     
  44. danbannana

    danbannana

    Joined:
    Aug 24, 2015
    Posts:
    4
    hmmmmm. the referenced script on this behaviour is missing warning is for the player. i dragged the right code in but it still didn't work. it says "the associated script can not be loaded. Please fix any compile error and assign a valid script."
     
  45. john1122

    john1122

    Joined:
    Aug 7, 2015
    Posts:
    3

    Thank you, Adam.
    I don't know why, but after I added a material to the walls, the error disappeared.
     
  46. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Like Maenor was saying, ensure that the actual script file name is EXACTLY the same in spelling and case as the Class name at the top of your script.
    for instance if your script is called PlayerController.cs
    within that script, ensure that the following class declaration in exactly the same spelling and case as the script filename
    ie
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    if you can, could you edit your code post, and use code tags please, as it doesnt show your code spacing etc correctly, and it makes it a bit easier to read.
     
  47. danbannana

    danbannana

    Joined:
    Aug 24, 2015
    Posts:
    4
    yes! thanks for that. i also fixed the problem with the pickups not being picked up. the code said Pick Up. in my game it was Pickup. ill make sure to post properly next time:D.
     
    OboShape likes this.
  48. Chee_F

    Chee_F

    Joined:
    Aug 30, 2015
    Posts:
    3
    Hi everyone,

    I followed the instructions on the tutorial and build this game for iOS but it would " quit unexpectedly" when I run it. Others seem to have encountered this problem before but I couldn't find a a relevant solution.

    I am running the game on a Macbook Pro with OS X Yosemite.

    I have also tried to run the game on a Windows laptop PC and it worked fine.

    Any ideas on what is going on or how I can fix this?
     
  49. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Just out of curiosity, from reading other places. what about trying to export all the assets as a bundle and import into a new clean project? never used IOS so just relaying something ive read.

    I take it theres no errors apparent, or errors logged in the game folder?
     
  50. Gammer23

    Gammer23

    Joined:
    Aug 31, 2015
    Posts:
    2
    Hi Im a newbie and having some trouble with the roll-a-ball game, basically whenever I press play my previous changes disappear, e.g i press play and my walls disappear along with my camera script, so the camera doesn't follow the ball, does anyone know the cause of this or have i just made a simple mistake, i save my scene and save with cmd+s but, like i said Im new at this, please help