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

Roll-a-Ball Tutorial Q&A [ARCHIVED]

Discussion in 'Community Learning & Teaching' started by Adam-Buckner, Mar 16, 2015.

Thread Status:
Not open for further replies.
  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Second: Your "Player" GameObject should be your sphere. If your "Player" is not the sphere...

    ... what is it?

    You may want to double check the steps where you create your player object. This is, iirc, creating the sphere object, renaming it "Player" and then attaching the "PlayerController" script to that sphere object.
     
  2. vorkas

    vorkas

    Joined:
    Mar 19, 2013
    Posts:
    27
    hey guys, great tutorial many thanks in advance! Your work is very well appreciated.
    I am having some trouble following though.. I am currently running unity 5. So my issue is to do with GUIText. After fiddling around with it for a while have managed to get GUI to appear on the screen but I don't understand how I can link my text to the player. In your video after completing the script, you drag and drop the created GUI text in the inspector component player in the box that appears after creating a public variable.. My issue is I cannot drag and drop it in place.. I could do this in the past with unity (was a version about 2 years ago cannot remember which one)..
    Any tips or suggestions?
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are there any errors that could be preventing you from doing this? Is your console clean?
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Do you have the "box" from the public variable? or no box?

    If you have the box, what is preventing you from dragging the GUIText object into the box?

    Are you sure you have the same "type" for each item? Is the variable GUIText in the script, and is the Component on the GameObject GUIText?
     
  5. DSmith15

    DSmith15

    Joined:
    Apr 9, 2015
    Posts:
    4
    I am so confused, it says I have no errors in my code and I have looked at other's code for Unity 5 and it seems to work but when I go into play mode nothing happens

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    public float speed;

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

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

    GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
    }
    }
     
  6. Adam-Buckner

    Adam-Buckner

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

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Is this script attached to a GameObject? Preferably your player GameObject ...
    Is the speed value set in the inspector? This should be the value set in the video ...

    !! Have you spelled everything correctly? No! Don't forget our languages are CaSEsEnsItIvE! You need to make sure everything you write is exactly like the example code.

    You need to change Fixedupdate to FixedUpdate. Note the capital U!
     
  8. Malin_92

    Malin_92

    Joined:
    Apr 9, 2015
    Posts:
    2
    Hello! I'm currently stuck on the collecting part. I keep getting the same two errors and though I've tried to solve them on my own I can't figure out what to do (I've doubled checked a few times to make sure I haven't misspelled anything, but if there's some obvious thing I've missed I apologize in advance). Please help me out!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.     void FixedUpdate ()
    9.     {
    10.         float moveHorizontal = Input.GetAxis ("Horizontal");
    11.         float moveVertical = Input.GetAxis ("Vertical");
    12.  
    13.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    14.  
    15.         if (movement != Vector3.zero)
    16.         {
    17.             GetComponent<Rigidbody> ().AddForce (movement * speed * Time.deltaTime);
    18.         }
    19.  
    20.         void OnTriggerEnter(Collider other);
    21.         {
    22.             if(other.gameObject.tag == "PickUp")
    23.             {
    24.                 other.gameObject.SetActive(false);
    25.             }
    26.         }
    27.      }
    28. }
    The errors are:
    • Assets/Scripts/PlayerController.cs(20,35): error CS1574: Keyword `void' cannot be used in this context
    • Assets/Scripts/PlayerController.cs(20,36): error CS1525: Unexpected symbol `(', expecting `)', `,',`;', `[', or `='

    PS. I know the ; in the void OnTriggerEnter(Collider other); wasn't in the video, but without it I got a Parsing errror
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    These errors seem to indicate that you are missing an expected character on lines before this code, and when the compiler reaches the code, even if it seems valid, the compiler is not expecting it and gets confused.

    So, back up one line or one character at a time looking for what's missing.

    So, what's missing?

    According to your code, OnTriggerEnter is inside the FixedUpdate function. Is this correct? As far as I know, that's not going to work.

    This is just plain wrong. You can't have that ; You need to figure out the real problem.
     
  10. DSmith15

    DSmith15

    Joined:
    Apr 9, 2015
    Posts:
    4
    The only thing I needed to do was change the "U" Thanks!
     
  11. Malin_92

    Malin_92

    Joined:
    Apr 9, 2015
    Posts:
    2
    Thank you so much! I'd been beating my head against a wall forever over this one.
    Everything works great now! You're the best!
     
  12. wolf3r

    wolf3r

    Joined:
    Apr 11, 2015
    Posts:
    9
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.    
    6.     public float speed;
    7.    
    8.     void FixedUpdate()
    9.     {
    10.         float moveHorizontal = Input.GetAxis ("Horizontal");
    11.         float moveVertical = Input.GetAxis ("Vertical");
    12.        
    13.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    14.        
    15.         if( movement != Vector3.zero) {
    16.             GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    17.         }
    18.     }
    19. }
    Hi i'm using this code and Unity 5 and I'm always getting the same error message.
    "The referenced script on this Behaviour is missing!"
    Then when I click on the script in the inspector there's another error message that says:
    "No MonoBehaviour script in the file, or their names do not match the file name." but If I'm right FixedUpdate is for example a Mono behaviour script.
    Any ideas?
    Thx
     
  13. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @wolf3r, just a thought while you wait for Adam to get back.

    is the player controller script, named exactly the same (as well as capitalisations) as the class name in your script

    ie 'PlayerController' is the name of the class in the script, and the exact same spelling and case is the script filename 'PlayerController'

    check that out, and if its different, remove and reattach the script to the player see how that goes. just a thought.
     
    wolf3r likes this.
  14. wolf3r

    wolf3r

    Joined:
    Apr 11, 2015
    Posts:
    9
    It worked, well my player is not moving yet but at least I'm not seing the error message thank you so much.
     
  15. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    cool :)

    just to mention, not sure if this is whats up or not, but if you remove and replace the script, the exposed 'speed' value you can see in the inspector will be reset back to whatever it was initialised to, in our case it will reset to 0. so if you haven't put this back up from 0 to what Adam had at 500, then the ball wont be going anywhere.
     
    wolf3r likes this.
  16. wolf3r

    wolf3r

    Joined:
    Apr 11, 2015
    Posts:
    9
    All right it's moving now. Thx
     
    OboShape likes this.
  17. wolf3r

    wolf3r

    Joined:
    Apr 11, 2015
    Posts:
    9
    Hi got a new problem, here's the code:
    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.     private int count;
    10.  
    11.     void Start ()
    12.     {
    13.         count = 0;
    14.         SetCountText ();
    15.     }
    16.  
    17.     void FixedUpdate()
    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.         if( movement != Vector3.zero)
    25.         {
    26.             GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    27.         }
    28.     }
    29.     void OnTriggerEnter(Collider other)
    30.     {
    31.         if (other.gameObject.tag == "PickUp")
    32.         {
    33.             other.gameObject.SetActive (false);
    34.             count = count + 1;
    35.             SetCountText();
    36.         }
    37.     }
    38.     void SetCountText()
    39.     {
    40.         countText.text = "Count:" + count.ToString ();
    41.     }
    42. }
    The error message is:
    "NullReferenceException: Object reference not set to an instance of an object
    Playercontroller.Start () (at Assets/scripts/Playercontroller.cs:14)"
     
  18. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Evening @wolf3r. just a question are you following the tutorial and using the GUIText component, or the new UI Text component to display the score (youll know its the new UI if you have a canvas component in the heirarchy)?
    just noticing you have the UI namespace included, and using the Text type as opposed to GUIText for your scorecount gameobject.

    just as a suggestion to check, can you ensure that you have dragged the counter gameobject from your heirarchy to your inspector slot so it knows what gameobject to use for displaying score.
     
    wolf3r likes this.
  19. wolf3r

    wolf3r

    Joined:
    Apr 11, 2015
    Posts:
    9
    Awesome. How can you even know what's the problem without watching my screen, and more difficult than that, How can't you get frustrated or upset with questions as stupid as this?
    I would, anyway thanks again.
     
  20. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Like to help out, as i know what its like to start out, having taken my first steps in unity last year :)

    and theres no such thing as a stupid question!
    it just means that when you come up against a similar issue later on, you will think "ah ive had something similar before, maybe its that.." and look to see.
    it all adds another little piece to the understanding jigsaw to get the bigger picture.
     
    Last edited: Apr 12, 2015
    wolf3r likes this.
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    @OboShape Thanks for keeping the watch-fires burning over the weekend.

    @wolf3r Glad you are making headway! There is no stupid questions to ask! Just ask!
     
    OboShape and wolf3r like this.
  22. wolf3r

    wolf3r

    Joined:
    Apr 11, 2015
    Posts:
    9
    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 GUIText countText;
    9.     public GUIText winText;
    10.     private int count;
    11.  
    12.     void Start ()
    13.     {
    14.         count = 0;
    15.         SetCountText ();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         float moveHorizontal = Input.GetAxis("Horizontal");
    21.         float moveVertical = Input.GetAxis("Vertical");
    22.      
    23.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    24.      
    25.         if( movement != Vector3.zero)
    26.         {
    27.             GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    28.         }
    29.     }
    30.     void OnTriggerEnter(Collider other)
    31.     {
    32.         if (other.gameObject.tag == "PickUp")
    33.         {
    34.             other.gameObject.SetActive (false);
    35.             count = count + 1;
    36.             SetCountText();
    37.         }
    38.     }
    39.     void SetCountText()
    40.     {
    41.         countText.text = "Count:" + count.ToString ();
    42.         if (countText.text = 12)
    43.         {
    44.             winText.text = "NOT BAD";
    45.         }
    46.     }
    47. }
    I'm not seing a Win Text value in the script component why?
    And I'm getting an error: Assets/scripts/Playercontroller.cs(42,31): error CS0029: Cannot implicitly convert type `int' to `string' line 42 is not as long but I was getting the same error in the line 41 before and there is where the ToString command is so I supose It'll be there
     
    Last edited: Apr 13, 2015
  23. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ok, you won't see any new functionality of a script in the editor until it has successfully compiled at least once.

    You are getting an error, which is preventing your script from compiling, so the new item won't be available to the editor and therefor the inspector until you fix the error.

    ... is saying that in the PlayerController script on line 41 there is an error of type CS0029 which is described as: Cannot implicitly convert type `int' to `string'.

    Oddly, I think the line numbers are off by one line - perhaps in pasting the code... ??

    I see an error in line 42: (which is of course the meaning of life and your solution) Here you are comparing a text property (countText.text [a string]) to a numerical value (12 [an int]). These are apples and oranges and cannot be compared, thus the error.

    You either need to compare a string to a string or an int to an int.

    I believe the video should have - int to int?
    Code (csharp):
    1. if (count >= 12)
    ... or something like that?
     
    wolf3r likes this.
  24. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    [Edited to keep thread clean due to double post]
    just another thing to watch out for as you go along, are your asignment and comparison operators, its easy to miss, and ive dont it on more than one occassion :)

    = this is the assignment operator, this assigns a right hand side value to the left hand side of the statement
    == this is the comparison operator it checks for equality between two similar types
    so what you have here
     
    Last edited: Apr 13, 2015
    wolf3r likes this.
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    And what he said^ about the == (^_^)
     
    wolf3r likes this.
  26. wolf3r

    wolf3r

    Joined:
    Apr 11, 2015
    Posts:
    9
    Ok Thanks for all :)
     
  27. Bill Nye the not so sciency guy

    Bill Nye the not so sciency guy

    Joined:
    Apr 14, 2015
    Posts:
    3
    Hey :D I made it past the coding part (Ca. 9:55) Bu when i press the "Play" button it won't move, can i change this in any way ?
     
  28. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    have you set the speed in the inspector?
    and check your FixedUpdate() has the correct capitalisation. and have a run through the script and check for other case sensitivity.

    unfortunately its hard to guess what is happening, if you can have a look to see if you are getting any errors please.
     
    Last edited: Apr 14, 2015
  29. Bill Nye the not so sciency guy

    Bill Nye the not so sciency guy

    Joined:
    Apr 14, 2015
    Posts:
    3
    Ahhh i figured it out :p it was a simple misspelling is all, sorry to bother. Althgough i have run in to another one (Figure that !) I cannot get the GUI-text to work, it just says: Assets/Scripts/PlayerController.cs(38,17): error CS0029: Cannot implicitly convert type `string' to `UnityEngine.GUIText. Do you know what to do ? Thanks in advance !
     
    Last edited by a moderator: Apr 16, 2015
  30. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Glad you found it, nearly missed your reply there, when replying just ensure that your additional message is below/outside the Quote tags, or it will collapse down with the quote, and people wont see it ;)

    can you copy and paste the section of player controller script code that is causing the problems please.
    (remember to use code tags too, makes it alot easier to read ;) its on the tool bar above on the right)
     
  31. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664

    My guess here is that you have written:
    Code (csharp):
    1. public GUIText winText;
    ... to declare the variable to hold the reference, and then you try to assign the text with:
    Code (csharp):
    1. winText = "";
    In this case, you need to realize that the property that you are setting is a member of the GUIText component, and you have to set that specific property with:
    Code (csharp):
    1. winText.text = "";
    Now, my example is a guess. You could have done this with countText or some other line in your code related to GUIText, but the pattern is the same. You need to set the GUIText.text property.

    Look at the GUIText component in the inspector, and you will see the "text" field as one part of the overall GUIText component.
     
    OboShape likes this.
  32. Bill Nye the not so sciency guy

    Bill Nye the not so sciency guy

    Joined:
    Apr 14, 2015
    Posts:
    3
    Hmmm. I might have misunderstood you (And yes the problem lies with counText sorry for not clarifying) You see whenever i try to put in the code (To my understanding it being: GUIText = "" ; ) i just get an error saying that the "=" was unexpected. If i show you the script it'll probably be easier to understand:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     GUIText = ""  
    7.     public float speed;
    8.     public GUIText countText;
    9.     private int count;
    10.  
    11.     void Start ()
    12.     {
    13.         count = 0;
    14.         setCountText ();
    15.     }
    16.  
    17.     void FixedUpdate ()
    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.         GetComponent<Rigidbody>().AddForce (movement * speed * Time.deltaTime);
    25.     }
    26.  
    27.     void OnTriggerEnter(Collider others)
    28.     {
    29.         if(others.gameObject.tag == "PickUp")
    30.         {
    31.             others.gameObject.SetActive (false);
    32.             count = count + 1;
    33.             setCountText();
    34.         }
    35.     }
    36.     void setCountText()
    37.     {
    38.         countText = "count: " + count.ToString ();
    39.     }
    40. }
    Thanks a lot for your patience and help :D
     
  33. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    First of all, it looks like you are deviating from what we've laid down in the tutorial, which means:


    Next:

    Remember when declaring a variable, you need to follow the pattern of:
    Code (csharp):
    1. access Type name;
    2.      // or
    3. access Type name = initial value;
    You have:
    Code (csharp):
    1. Type = initial value;
    You can't set a class type to a value.

    What is even more important is to remember about class members. As stated above, you have to address the members within a class if you want to set those members.

    For out GUIText items, the text variable is a member of the GUIText class, so you have to address it with:
    Code (csharp):
    1. public GUIText myText;
    and somewhere in your code, perhaps Awake() or Start():
    Code (csharp):
    1. myText.text = "Some string value";
    Note your line 38: Here again, you are trying to assign a value directly to the GUIText class, not the member.
     
Thread Status:
Not open for further replies.