Search Unity

Official Roll-a-ball Tutorial Q&A

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

  1. Descenter

    Descenter

    Joined:
    May 24, 2015
    Posts:
    1
    First off - THANKS for making the tutorials! -Its great fun!

    This is my first time on Unity 5 (was playing around a bit with 4)
    Upon startup my Unity 5 looks nothing like in yout video!
    Dont know if its an update thing, og you just made it look like that for your personel pref.
    But it sure dont make it any easyér to follow!
    Well, after som playing around I made it look a little like in the video!
    What I cant get to work is that my scene veiw ONLY shows the ONE item i highlight from the Hierachy,
    and only in wireframe (and yes, it is set to shaded in the scene?)

    Now up until lesson/video 3 everything works as intended!

    In lesson/video 3 Im adding a script component to the Main Camera "the CameraController"
    this script is the exact same as you did in the video!

    When saved it says:
    "The referenced script on this Behaviour is missing!"
    In the Inspector it says:
    "The associated script can not be loaded.
    Please fix any compile errors
    and assign a valid script."

    The line player in the inspector never appears,
    and therfore I can´t drag the player object to that line/float.

    I have done this lesson 3 times now, and always get stuck here. Dont know If Im doing somthing wrong!
    My two scripts, and yes they are attached to the player object and the camera (everything is done as in the video)

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

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

    rb.AddForce (movement * speed);
    }
    }
    -------------------------------------------------------------------------------------------------------
    using UnityEngine;
    using System.Collections;

    public class CamerController : MonoBehaviour {

    public GameObject player;

    private Vector3 offset;

    // Use this for initialization
    void Start () {
    offset = transform.position - player.transform.position;
    }

    // Update is called once per frame
    void LateUpdate () {
    transform.position = player.transform.position + offset;
    }
    }
    ------------------------------------------------------------------------------------------------------

    (also; How do i get Unity 5.0.0f4 (64-bit) to look and behave excatly like you have it in you videos?
    That would also help alot! Thanks ALOT!

    upload_2015-5-24_2-52-55.png
     
  2. thelink19

    thelink19

    Joined:
    May 21, 2015
    Posts:
    4
    I have written this code
    Code (CSharp):
    1. void FixedUpdate()
    2.     {  
    3.         rb = GetComponent <Rigidbody> ();
    4.         float moveHorizontal = Input.GetAxis("Horizontal");
    5.         float moveVertical = Input.GetAxis("Vertical");
    6.  
    7.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    8.  
    9.         rb.AddForce(movement);
    10.     }
    It works now, but I don't understand a few things.

    1)Why, in the code of this page https://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player, rb is not used in the FixedUpdate function?
    2)What is the scope of the start function?
     
    Last edited: May 25, 2015
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    A couple of things...

    First off, I would watch some of the videos in the learn section of the website that are related to the editor. You can also familiarise yourself with the editor by reading the relevant pages in the manual. The reason I suggest this is there are quite a few settings to check to make sure the editor is laid out and displaying the same views. It would be easier to cover these on your own and the ping us if you get stuck. This includes wireframe vs shaded mode, window layout, etc..

    When it comes to the "referenced behaviour is missing" error - this means that you have added a script to a GameObject, and later that GameObject has lost the reference to the script. A common way of losing the reference is to delete the script and remake it. In this case the newly remade script is not associated with the empty script component on the GameObject. You should drag the new script onto the script component or delete the broken script component and drag the desired script onto the GameObject for a fresh start. Be careful you don't have too many copies of the script on the GameObject.

    That is what these errors are saying:
    If you are going to post code, please learn to format your code using code tags. It can look like this:
    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. }
     
  5. SmitSheth

    SmitSheth

    Joined:
    May 27, 2015
    Posts:
    2
    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 moveVertical=Input.GetAxis("Vertical");

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

    rb.AddForce(movement*speed);

    }
    }


    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:20)


    How to solve this exception??
     
  6. 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/

    Because your code is not propertly formatted, I cannot see the line numbers so I cannot tell which line is causing the error.

    Please format your code properly and ask the question again.
     
  7. SmitSheth

    SmitSheth

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



    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:20)
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Line 20 has a null reference exception. This means the reference to another object, in this case "rb", is Null or missing / empty / does not exist. The reference variable is there, but it does not contain a valid reference.

    You need to track back and find out why this reference is missing.

    You set the reference in start () with a GetComponent call. This looks for a component of the type specified on the same game object.

    Your missing reference could be because there is no valid rigidbody attached to the same game object, so the GetComponent fails to find the desired component.

    Or is could be that the GetComponent is never called...

    Or potentiually even some other issue...

    In your case, GetComponent is never called.

    C# (and UnityScript) are CaSe SenSitiVe languages.

    You have written start, not Start, so the function is never called. Unity is looking for any function specifically called "Start", in this case, to call. There is no "start" function that Unity calls automatically... therefore you have no valid reference, just an empty variable.
     
  9. Blinciki

    Blinciki

    Joined:
    Mar 30, 2015
    Posts:
    1
    Hello,

    Thank you for tutorial project, it was really fun and it covered a lot material. It is more engaging to do a project, in comparison to just watch a video about some feature.

    I wanted to mention, that I kept receiving some errors along the way, though the script was exactly like in the videos. I just restarted the software. After the restart, there was no error. I had errors, only in case I had made some typos. But in rest everything worked.

    I think it's worth mentioning to be attentive at scripts, because C# in case sensitive , and to not forget to link the scripts to the relevant objects.

    Thanks, again.
     
    Last edited: Jun 1, 2015
  10. xdzzz

    xdzzz

    Joined:
    Jun 18, 2014
    Posts:
    1
    Awesome tutorial, just finished it. I appreciate the attention to detail when it came to scripting.
    The scripting API is fantastic with simple, elegant examples. Thanks for emphasizing good habit by looking up all classes and their functions.

    I will use this project as a springboard into the others. I plan to begin experimenting with this too.
    I might try to make a PacMan and/or a SuperMonkeyBall clone with this build.
    Just for funsies :D

    Many thanks from a newbie.

    P.S. I did not come across a single error besides "inconsistent line endings" -- which VisualStudio automatically corrected to Windows format for me anyways. Not a big issue.
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yes! Thanks for reinforcing this.

    C# and Unity's JavaScript/UnityScript are CaSe SentiTive! And you must remember to make your references! Either by dragging them into a public variable in the inspector, or with a FindGameObject/GetComponent in code.
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Re: the line endings warning - Yes, the line ending thing always seems to generate a warning, but (afaict) doesn't actually seem to cause any issues. I always clean them up. As we author on both Mac and Win OS, and our target audience is on both (and some wine/cider users on Lin) there is little we can do to avoid this issue.
     
  13. Yokagama

    Yokagama

    Joined:
    Jun 1, 2015
    Posts:
    1
    There is a bit mistake in the scrypt "PlayerController" bellow the 7-th tutorial video "Collecting the Pick Up Object"

    wrong 27 line: if (other.gameObject.tCompareTag ("Pick Up"))

    correct 27 line: if (other.gameObject.CompareTag ("Pick Up"))
     
  14. JohnFWhitesell

    JohnFWhitesell

    Joined:
    Jun 1, 2015
    Posts:
    2
    I get null reference errors whenever I try to access Text variables. I tried copying the code in the code box from tutorial 7 and that code gives me null reference errors as well.
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Null Reference errors are not necessarily bad code.

    Null Reference errors mean that you are missing a reference to an object.

    There are several ways this can happen.

    One of the most common is that the code defines a public variable that must be populated in the inspector. If the user doesn't drag the game object into the slit created by the reference variable in the inspector, then there will not be a viable reference to any object.

    Remember you must follow all the steps in the video, including ones related to the unity editor.
     
  16. JohnFWhitesell

    JohnFWhitesell

    Joined:
    Jun 1, 2015
    Posts:
    2
    Yeah turned out I needed to stop looking everywhere and just sit back and watch the thing again.

    I'm used to coding in pure text where I can scan my programs line by line so unity can seem a bit full of nooks and crannies at times. Is there any way to display the entire game world in giant blocks of script for a grognard?
     
  17. Amoebozoa

    Amoebozoa

    Joined:
    Jun 2, 2015
    Posts:
    19
    Greetings, thanks for a great tutorial, I just started learning unity after making 2D games in Gamemaker for fun so now I decided to step up my game a bit, now I seem to have run into a bit of a pickle, I simply can not get the bloody ball to move, I tried resetting the Rigidbody, double and tripplechecked my code, I fixed the bits that were broken before and it's still not moving.

    Ontop of that the tutorial website is absolutely broken, ( http://i.imgur.com/zPwB5I7.jpg ) as it adds a bunch of text everytime you click anywhere on the website, atleast for me.

    And what you should do is tell people how to get into the input window where they can change and select their keys, I had no idea where it was I had to find it for myself.

    I'l add my code if anyone wants to have a look, I'l continue to try and figure out why it ain't moving

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

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    No, not really. You can, if you wanted to, write your game entirely in code and never use the editor, but this will bypass some of the most amazing things about Unity.
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    FixedUpdate is misspelled as fixedupdate. Unity's programming languages are CaseSensitive, and this makes a different name for the function. Unity will be calling FixedUpdate and not fixedupdate.

    Have you tried a different browser when reading the learn site? I can't reproduce this issue. If the website continues to be an issue, please role a bug report with all the details of operating system and browsers used.

    Lastly, when it comes to the Input Manager and changing keys, we decided to limit the tutorial's scope and did not mention this on purpose. The current settings "just work" and how to change these are in other lessons.
     
  20. Amaan-Sadri-Wala

    Amaan-Sadri-Wala

    Joined:
    Jun 9, 2015
    Posts:
    14
    I saw the second video moving the player i entered the same c# script as you and i tried a thousand times but it always say "Please fix any compile errors and assign a valid script" what should i do?
    i need help coz i'm a total noob i use unity 5.0.2f1 64bit
     
    Last edited: Jun 9, 2015
  21. Relai87

    Relai87

    Joined:
    Jun 9, 2015
    Posts:
    3
    Hello,

    I just completed the 7th part, all worked fine. Then I thought lets make the winner text a little bigger, so I changed the Font Size to 30, but then my winner text disappeared. Is Unity´s font size limited? It works with font size 26 and any less than that, but more than 26 doesnt seem to work =/ And if I use scaling the text looks pixly.
    PS: Also tried using Pixel Perfect on the canvas, not the result I wanted.

    @Amaan Sadri Wala
    When you saved your script in monodev go to Unity again you will see a line at the bottom, double click it and you will see your errors. You probably just typed something wrong. Just follow the video one more time or go to the first video of the series if you didnt yet.
     
    Last edited: Jun 10, 2015
  22. alessandro78

    alessandro78

    Joined:
    Aug 4, 2014
    Posts:
    5
    I'm face the same issue as most of the guys hire..
    not able to move the ball using the keyboard when i run the game

    post my code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     private Rigidbody rb;
    8.     public float speed;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         rb = GetComponent<Rigidbody>();
    13.     }
    14.  
    15.  
    16.     //update before physic calculation
    17.     void FixedUpdate () {
    18.  
    19.         float moveVertical = Input.GetAxis ("Vertical");
    20.         float moveHorizontal = Input.GetAxis ("Horizontal");
    21.  
    22.         Vector3 move = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    23.         rb.AddForce (move * speed);
    24.     }
    25. }
    26.  
    i already checked the inspector that is correctly valorized, from the immage you can see that my speed is set to 100

    speed.png


    i tried different solutions, also i tried to revalorize in the void FixedUpdate ()
    the variable rb calling the GetComponent
    rb = GetComponent<Rigidbody>();

    but nothinng is changed for me....
     
  23. Relai87

    Relai87

    Joined:
    Jun 9, 2015
    Posts:
    3
    Is Kinematic, try to uncheck it. I wonder why it is checked in the first place^^

    "If enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform. This is useful for moving platforms or if you want to animate a Rigidbody that has a HingeJoint attached."
    We do not use transform here, so it is no wonder it does not work. With Kinematic it is just an object in space that is not moved except we apply transform. I am also new to this - only read the documentation, hope the explanation was good enough.
     
    Last edited: Jun 10, 2015
    alessandro78 likes this.
  24. alessandro78

    alessandro78

    Joined:
    Aug 4, 2014
    Posts:
    5
    thanks it works!!!!! i spent two hours to make it works and was just that flag....
    but what is that for?
     
  25. Relai87

    Relai87

    Joined:
    Jun 9, 2015
    Posts:
    3
    Edited my post.
     
  26. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Without being rude: You need to fix any compile errors and assigna valid script.

    Now, what exactly is making your script invalid, I'm not sure. It also could be that you have not assigned the script, or have deleted a script that needs to be assigned.

    Are there any other errors in your console?
     
  27. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you make your text larger than the container, it is possible it won't be displayed.

    The solutions to this are to either make the container bigger, or to change the "Overflow" for the text component so that the text is allowed to grow outside of the container.

    http://docs.unity3d.com/ScriptReference/UI.Text.html
    http://docs.unity3d.com/ScriptReference/UI.Text-verticalOverflow.html

    (Oddly the page on horizontalOverflow - which works - is not in the documentation!)
     
    Relai87 likes this.
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Seems that you've figured it out! Yes, don't check "Is Kinematic"!

    This is a setting that allows the rigidbody to particiapte in some parts of the physics engine, like collisions, but ignore physics forces, like AddForce, or Gravity.
     
    alessandro78 likes this.
  29. alessandro78

    alessandro78

    Joined:
    Aug 4, 2014
    Posts:
    5
    Hallo guys

    something that i don't understand how mange regardin this section
    DISPLAYING THE SCORE AND TEXT

    actually when i add the text componet unity create for me the Canvas and the event system

    the orientation of the Canas start from the origin of the axis and to same positive x and y
    as you can see the canvas is huge and the text element need to be oriented inside the canvas

    now i wolud like to oriented the canvas in the way to have the Text component in same place on the centre of the game area

    but the Rect Component of the Canvas is disable, I mean i cannot change the values by the input text
    so i tried to traslate the canvas using the traslate palette but from them i can move only by the Z axis the other two are blocked

    same one can explane my how to redimension the canvas and traslate? canvas.png


     
  30. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Hi @alessandro78 bear in mind that the canvas is like an overlay to your scene and covers the visible viewing area from your camera.

    For more info relating to the canvas and to get a feel for how it interacts with your game, have a look at the UI tutorials Adam has done http://unity3d.com/learn/tutorials/modules/beginner/ui

    This should give you a better understanding on Text element placement etc and give you an idea of how this works.

    Just as an addition, when working with the UI elements etc its best to work in 2D mode. There's a 2D toggle at the top left-ish of the editor window, but the tutorials mentioned above will explain this.

    Hope that helps a wee bit.
     
    Last edited: Jun 13, 2015
    Adam-Buckner likes this.
  31. alessandro78

    alessandro78

    Joined:
    Aug 4, 2014
    Posts:
    5
    mmm... this not help a lot me :) but anyway i will follow your advice to view the tutorial
    may be this will clear my lack in the concept related to the relationsheep between the canvas and the visible area

    thank you anyway
     
  32. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You are looking at the RectTransform of the Canvas, which you have no control over. The canvas, in overlay mode as you have it, will automatically be scaled to the size of your game's viewport. You need to position the text element inside the canvas. As the canvas is the same as the viewport, by positioning the text in the canvas, you are positioning the text in the viewport.
     
  33. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    Hi everyone I'm a complete noob to unity. I tried using a year back and got frustrated with it. but I thought I would try again, anyway.
    I am on lesson 3 (Moving the Player) at 12:48 on the video he has all of his code put in for the first time and tests. With this he is able to move the ball. I have followed along but when I got to test... nothing the ball does not move. I am not getting any error codes or anything like that. Here is my code.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    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);
    23.     }
    24. }
    25.  
    When I hit the play button it plays but nothing seems to happen, not even an error code. If anyone could give me a hand that would be awesome.

    Edit : I did have Microsoft Visual Studios Unity set up for doing the code but when I thought this may have been the issue I put default back to MonoDevelop.

    Edit#2 : never changed my code or anything that I am aware of but now it's working.
     
    Last edited: Jun 18, 2015
  34. originalself

    originalself

    Joined:
    Nov 13, 2014
    Posts:
    2
    Hello. I am following along with this tutorial and I am having problems at the adding movement portion. I am getting the following errors in my console:

    Assets/scripts/PlayerController.cs(18,71): error CS0103: The name `moveVerticle' does not exist in the current context
    Assets/scripts/PlayerController.cs(18,84): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments
    Assets/scripts/PlayerController.cs(18,84): error CS1503: Argument `#3' cannot convert `object' expression to type `float'
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. Here is my script:
    4. [code=CSharp]using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class PlayerController : MonoBehaviour {
    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, moveVerticle);
    22.  
    23.         rb.AddForce (movement);
    24.     }
    25. }
    Thanks in advance for the great tutorials and any help.
     
  35. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    It is possible to have correct code, but still have problems with your game, because the game objects and components are not constructed correctly in the scene. You need to check things like: Is your script attached to your game object? Are there any errors in the console? Have you assigned a value to speed in the inspector? Have any of the components changed? (e.g.: have you accidentally selected is kinematic on the rigid body?) double check all of your steps against those in the video. You can check your scene against the "done" seen in
     
  36. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The answers are in the error messages themselves. The following two errors are what I like to call a "Cascade of errors" which is that because of the first error, the other two have been created.

    Where in your script have you declared the variable "moveVerticle"?
    You haven't.

    This is a misspelling of "moveVertical".

    As you have not declared "moveVerticle", then Unity does not know what type of variable "moveVerticle" is. This leads to the Vector3 being wrong as the only way that Unity can interpret "moveVerticle", is as a generic object, and the Vector3 is supposed to be a float, float, float - not a float, float, object.
    This leads to Unity trying to convert the object into a float, which I can't do, so there is another error.
    Arguably, all three of these errors will help you find and fix the issue. It tells us that on line 18, we have an unknown variable, and that the error is in the Vector 3, and lastly that it's the Argument #3 that is considered and object, not a float.
     
  37. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    Thanks for the help I actually did get it working yesterday. I never changed anything in it I left it for a few hours came back and tried it again and it just worked.

    Question on scripting!

    In the script when it uses
    Code (CSharp):
    1. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    Does this mean we are creating a new "item" Vector3 and giving it a name movement.
    and then next line calling that named Vector3 using
    Code (CSharp):
    1. rb.AddForce (movement);
    (really new to this and need a little clarification)

    Thanks

    WAX
     
  38. originalself

    originalself

    Joined:
    Nov 13, 2014
    Posts:
    2
    Thank you. Easy fix and it runs now. I love the way it automatically uses so many input options. Even my gamepad works in testing.
     
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yep. You got it. the "item" is usually called an "object" as this is technically "Object Oriented Scripting".

    So, you create a new "object" called "movement" and give it the Type "Vector3" with the value of moveHorizontal, 0.0f, moveVertical to go into the Vector3's x, y & z properties using:
    Code (CSharp):
    1. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    Now that it exists and contains some data, you use it with:
    Code (CSharp):
    1. rb.AddForce (movement);
    Now, I believe you could also write:
    Code (CSharp):
    1. rb.AddForce (new Vector3 (moveHorizontal, 0.0f, moveVertical));
    ... but I find this is harder to read. In the original way, this also allows you to reuse "movement" in later code, as long as it's within the "scope" of where it was created - in this case the FixedUpdate function.
     
  40. Kaziarl

    Kaziarl

    Joined:
    Apr 23, 2015
    Posts:
    10
    Hello folks.

    In a post, oh... a month or two ago? I was talking about expanding on this game and recreating one of those old wooden tilt marble mazes. I had some problems at the time, but I've worked them out and everything works. Now it's on to building the level. Now, I could build the levels by hand (and probably will for the first one, for now) but I was wondering if anyone could suggest a good way to randomly generate the maze? I'm still fairly new to scripting, but I was thinking of using part of the script from the 2D Roguelike that is used to generate each level there. Is that possible? Will that cause any problems beyond it being 2D vs 3D?

    Edit: Also, I don't really know my way around the forums here yet. Is there a better place to ask about this? I figured I should find out since this is beyond the scope of the tutorial.
     
    Last edited: Jun 22, 2015
  41. Ambroz

    Ambroz

    Joined:
    Jun 23, 2015
    Posts:
    1
    Hi everyone. I'm new at Unity and I would want to ask you, how to make the sphere(ball) jump by pressing the Space key. I would need to change the Y to some value and than make it back to the default. Could you tell me how to do it? I would be very pleased.
     
  42. Kaziarl

    Kaziarl

    Joined:
    Apr 23, 2015
    Posts:
    10
    Hi Ambroz.

    This is just a quick and dirty, and probably not the best way to go about it as I'm still a bit of a noob. But here's what I worked out.

    I added:

    Code (CSharp):
    1. public float jumpSpeed;
    2. private bool hasJumped = false;
    3. private bool isGrounded = true;
    to the top of the player controller with speed, winText, and the others.

    Then I added:

    Code (CSharp):
    1. void Update (){
    2.         hasJumped = false;
    3.         if (Input.GetKeyUp (KeyCode.Space)) {
    4.             hasJumped = true;
    5.         }
    6.  
    7.     }
    And in fixedUpdate I added:

    Code (CSharp):
    1. if(hasJumped & !isGrounded){
    2.             rb.AddForce(Vector3.up * jumpSpeed);
    3.             hasJumped = false;
    4.             isGrounded = false;
    5.         }
    Finally, I added:

    Code (CSharp):
    1. void OnCollisionEnter (Collision collision){
    2.         isGrounded = false;
    3.     }
    Then in the Unity screen, on the player controller component of the player sphere I set the jumpSpeed to 500.

    It could use some work. For one thing, I can just keep hitting the space bar and it will keep going higher and higher. At which point when you end up falling so fast that you end up falling through the plane. But it's a start, and hopefully it helps you out.
     
  43. MarioMetroid

    MarioMetroid

    Joined:
    Jun 25, 2015
    Posts:
    1
    Hello! I just installed Unity yesterday and did this tutorial. I do have a question though. I tried to add some rolling cylinders that, if touched by the player, would result in a game over. I want them to move either left and right or up and down, collide with the walls, and not collide with the pickups (since they roll over at least 1 of them).

    I know I need a script to make the cylinders rigidbodies and have them roll around.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CylinderHorizontalRoll : MonoBehaviour {
    5.  
    6.     public float speedcylinder;
    7.     private Rigidbody rb;
    8.  
    9.     void start()
    10.     {
    11.         rb = GetComponent<Rigidbody> (); //want a rigid body so it can collide with the walls
    12.     }
    13.  
    14.         // Update is called once per frame
    15.     void FixedUpdate ()
    16.     {  
    17.         Vector3 movement = new Vector3 (0,0,2);
    18.  
    19.         rb.AddForce (movement * speedcylinder);
    20.         //transform.Translate (new Vector3 (0,0,2) * Time.deltaTime); //does not collide with wall, keeps moving to the right
    21.     }
    22. }
    I'm trying to give them a static force in one direction, just to get them to roll in one direction before I worry about them rolling in the other direction. But I get the error on the line 19 (rb.AddForce....) "NullReferenceException: Object reference not set to an instance of an object." And the cylinders do not move at all.

    Any ideas how I can fix this?
     
  44. krimzero1

    krimzero1

    Joined:
    Jun 26, 2015
    Posts:
    2
    Hi im just having one problem, when i build the game the ball won't collect any of the cubes in game. it just goes right through them. But it works just fine in scene and when i test it. If you can help? Thanks
     
  45. Jarkiboy

    Jarkiboy

    Joined:
    Jun 27, 2015
    Posts:
    2
    I have an Error:
    Assets/Scripts/PlayerController.cs(4,14): error CS0101: The namespace `global::' already contains a definition for `PlayerController'

    what does it means an how can i fix it?
     
    Last edited: Jun 27, 2015
  46. Kaziarl

    Kaziarl

    Joined:
    Apr 23, 2015
    Posts:
    10
    Hi Jarkiboy.

    It would help if we could see the code. Would you mind copying for us? Be sure to use the code tag.
     
  47. Jarkiboy

    Jarkiboy

    Joined:
    Jun 27, 2015
    Posts:
    2
    Oh I have fixed it ^^

    I just renamed the Script :)
     
  48. Kaziarl

    Kaziarl

    Joined:
    Apr 23, 2015
    Posts:
    10
    Awesome! :D
     
  49. justasxz

    justasxz

    Joined:
    Jul 1, 2015
    Posts:
    1
    guys i got a problem ball wont bounce all the time and rotating speed doesnt change speed at all i bumped it up too 1k its still the same (tried from both script and unity) this is my code
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var rotationSpeed = 200;
    4. var speed : float = 10.0;
    5. var jumpHeight = 8;
    6. private var po = true;
    7. var h1 : AudioClip;
    8. var h2 : AudioClip;
    9. var h3 : AudioClip;
    10.  
    11. private var isFalling = false;
    12.  
    13. function Update () {
    14.  
    15. // makes ball rotate
    16.  
    17. var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
    18. var translation : float = Input.GetAxis ("Vertical") * speed;
    19. translation *= Time.deltaTime;
    20. rotation *= Time.deltaTime;
    21. GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
    22. transform.Translate (0, 0, translation);
    23.  
    24. if (Input.GetKeyDown(KeyCode.Space) && isFalling == false)
    25. {
    26.  
    27. GetComponent.<Rigidbody>().velocity.y = jumpHeight;
    28. playOnceTrue();
    29.  
    30. }
    31. isFalling = true;
    32.  
    33. }
    34. function OnCollisionStay ()
    35. {
    36. if (po == true){
    37.  
    38. var hit = Random.Range(0,4);
    39. if (hit == 0){
    40. GetComponent.<AudioSource>().clip = h1;
    41. }
    42. else if(hit == 1){
    43. GetComponent.<AudioSource>().clip = h2;
    44.  
    45. }
    46. else{
    47. GetComponent.<AudioSource>().clip = h3;
    48.  
    49. }
    50. GetComponent.<AudioSource>().pitch = Random.Range (1, 3);
    51. GetComponent.<AudioSource>().Play();
    52. po = false;
    53.  
    54. }
    55. isFalling = false;
    56. }
    57. function playOnceTrue() {
    58. yield WaitForSeconds (0.2);
    59. po = true;
    60.  
    61.  
    62. }
     
  50. ChrisKallenar

    ChrisKallenar

    Joined:
    Jul 1, 2015
    Posts:
    1
    Hey guys! Complete newbie here, both to Unity and scripting.

    I finished the tutorial without too much problem, and decided to try and add a few things to make it more interesting. And I ran into some problems, that I've been banging my head on for the past 5 hours now. Since I'm not sure what to actually search for on the forums/google etc, I've hit a dead end.
    (If this is the wrong forum for it, please point me in the right direction.)

    I've added a particle system to the pick-up objects, so when the ball rolls over and "collect" them, a puff of smoke appears.
    The problem is that the particle clones stay in the hierarchy during the game, and I have no idea how to delete them.
    (They only appear when in “play mode”, and disappear when you exit play mode)

    I might have committed one or several cardinal sins of coding. But until 2 days ago, I had never written a single line of code in my entire life.

    Here is a screenshot of it in play mode, and the 2 scripts that I'm using:
    For the "PlayerController":
    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.     public Text countText;
    9.     public Text winText;
    10.     private int count;
    11.  
    12.     void Start ()
    13.     {
    14.         count = 0;
    15.         SetCountText ();
    16.         winText.text = "";
    17.     }
    18.  
    19.     void FixedUpdate ()
    20.     {
    21.         float moveHorizontal = Input.GetAxis("Horizontal");
    22.         float moveVertical = Input.GetAxis ("Vertical");
    23.    
    24.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    25.    
    26.         GetComponent<Rigidbody>().AddForce (movement * speed);
    27.            
    28.         {
    29.             if (Input.GetKeyDown ("space") && GetComponent<Rigidbody>().transform.position.y <= 0.6250001f) {
    30.                 Vector3 jump = new Vector3 (0.0f, 200.0f, 0.0f);
    31.            
    32.                 GetComponent<Rigidbody>().AddForce (jump);
    33.             }
    34.         }
    35.    
    36.     }
    37.  
    38.     void OnTriggerEnter(Collider other)
    39.     {
    40.         if (other.gameObject.CompareTag ("Pick Up"))
    41.         {
    42.             other.gameObject.SetActive (false);
    43.             count = count + 1;
    44.             SetCountText ();
    45.  
    46.         }
    47.  
    48.         if (other.gameObject.CompareTag ("Restart"))
    49.         {
    50.             Application.LoadLevel (Application.loadedLevel);
    51.         }
    52.     }
    53.    
    54.  
    55.     void SetCountText ()
    56.     {
    57.         countText.text = "Count: " + count.ToString ();
    58.         if (count >= 4) //Change to actual number of pickup objects
    59.         {
    60.             winText.text = "You Win!";
    61.  
    62.         }
    63.     }
    64. }
    And for the "Rotator":
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Rotator : MonoBehaviour {
    5.  
    6.  
    7.     //The code for rotating the cubes in the tutorial
    8.     void Update ()
    9.     {
    10.         transform.Rotate (new Vector3 (15, 30, 45) * Time.deltaTime);
    11.     }
    12.  
    13.     //The particle system code I use
    14.  
    15.     public ParticleSystem coinSmoke;
    16.  
    17.  
    18.     void Awake ()
    19.     {
    20.         coinSmoke = (ParticleSystem)Instantiate(coinSmoke);
    21.         coinSmoke.transform.position = this.transform.position;
    22.     }
    23.  
    24.  
    25.     void OnTriggerEnter(Collider other)
    26.     {
    27.         if (other.gameObject.CompareTag ("Player"))
    28.         {
    29.             coinSmoke.Play ();
    30.         }
    31.     }
    32.  
    33. }
     

    Attached Files: