Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Following a tutorial, cannot figure out what's wrong

Discussion in 'Scripting' started by C4de, Jan 7, 2014.

  1. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    I'm following the "Roll-a-Ball" tutorial and I've run headfirst into a brick wall. I've typed up the code just like the tutorial said, and I'm ready to test it, but I can't.

    When I try adding it to the player object, it says that the code isn't done compiling yet and I should wait. If I just try to press play, I get this error:

    Assets/Scripts/PlayerController.cs(4,14): error CS0101: The namespace `global::' already contains a definition for `PlayerController'


    What am I doing wrong?
     
  2. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
  3. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    Just to second this. Finished helping out someone yesterday, and after much much much pondering and failing, I went over the scripts myself and then finally found the causes of the issues.
     
  4. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    Sure, here it is:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     void FixedUpdate()
    7.     {
    8.         float moveHorizontal = Input.GetAxis("Horizontal"):
    9.         float moveVertical = Input.GetAxis("Vertical)":
    10.  
    11.          Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical):
    12.        
    13.          rigidbody.AddForce(Vector3);
    14.     }
    15. }
     
  5. jgodfrey

    jgodfrey

    Joined:
    Nov 14, 2009
    Posts:
    564
    The error indicates that there is already a symbol named "PlayerController" in the global namespace. So, in the above script, you've create a class named "PlayerController", but that name has already been taken by something else. I'm not familiar with the tutorial you're following, but the error is fairly clear. You can't have two things named PlayerController in the same namespace.

    Jeff
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,895
    Is it possible you just typed in two copies of the script into separate files in different folders of your project??

    It sounds like there are two different source files that define the PlayerController class.

    I think also that some of the Unity standard modules use that name, so perhaps change it to something else (MyPlayerController) and make sure that's what you have attached to the object you want it attached to.

    Kurt
     
  7. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    I found the extra PlayerController script and deleted it. Then I fixed another error, but now it's giving me a third:

    Assets/Scripts/PlayerController.cs(13,37): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    What does THAT mean?

    This is the new version of the script, for reference:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     void FixedUpdate()
    7.     {
    8.         float moveHorizontal = Input.GetAxis ("Horizontal");
    9.         float moveVertical = Input.GetAxis ("Vertical");
    10.  
    11.         Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
    12.        
    13.          rigidbody.AddForce(Vector3);
    14.     }
    15. }
    Did I put a space somewhere I shouldn't have?



    The tutorial I'm following is here:

    http://unity3d.com/learn/tutorials/projects/roll-a-ball
     
  8. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    You need to put in a proper value as an argument for the function AddForce. Vector3 is a datatype. I believe the variable you want to use is movement.
    Code (csharp):
    1.  
    2. rigidbody.AddForce(movement);
    3.  
     
    snezyc likes this.
  9. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    I fixed that, now it's telling me that "}" is unexpected.

    This is the exact error message:

    Assets/Scripts/PlayerController.cs(14,9): error CS1525: Unexpected symbol `}'
     
    Last edited: Jan 7, 2014
  10. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    What is your updated code? It looks like you might have missed a semi-colon or deleted a closing bracket.
     
  11. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    This is the very latest version of the script.:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     void FixedUpdate()
    7.     {
    8.         float moveHorizontal = Input.GetAxis ("Horizontal");
    9.         float moveVertical = Input.GetAxis ("Vertical");
    10.  
    11.         Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
    12.        
    13.         rigidbody.AddForce(movement)
    14.     }
    15. }
    I've tried going over it again myself, but I'm absolutely stumped.
     
  12. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    You have missed a semi-colon at line 13 at the end of AddForce.
    Code (csharp):
    1.  
    2. rigidbody.AddForce(movement);
    3.  
     
  13. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    ...

    /headdesk

    Thanks, that fixed it completely.

    Is that a common mistake?
     
  14. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    It's a common newbie mistake. And usually the one that makes us go "Duh!" and have a noob moment. It's not such a big deal, once you've gotten used to coding, semi-colons will become part of your second nature.
     
  15. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    Well that's good to know.

    Just one more question.

    How do I make Monodevelop stop "completing" what I type? I've tried to type "offset" and "private", and it "completed" them half a dozen times, creating things I didn't need and forcing me to go back and try to type it again.

    The only way I've found to circumvent that "feature" is to type of the word, click someone where in the script, and return to finish it.

    It's very frustrating.
     
  16. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    You often get this sort of thing when you are on the slow train to noobsville.

    Everybody gets them and errors like this usually mean you have missed a bracket or comma or semicolon.

    This error was on line 14 (shown by (14,9) in the error) so by looking at the previous line 13 you can see the missing ; as wolfhunter777 pointed out.
     
  17. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Tools->Options->Text Editor->Enable Code Completion

    That should do it. However, I find when you get used to the language and functions it is a pretty useful tool.
     
  18. C4de

    C4de

    Joined:
    Jan 6, 2014
    Posts:
    9
    Thanks, that should help a lot.
     
  19. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    You can press 'escape' when the code completion box pops up to get rid of it.

    I agree with Duney it is very useful at times and saves a lot of time.
     
  20. SteroidCookies

    SteroidCookies

    Joined:
    Mar 9, 2015
    Posts:
    1
    Can some1 pls help me?
    When i start it it says:
    "Assets/Scripts/PlayerController.cs(24,21): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected"

    and this is the code,but it doesnt show any errors:

    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof(CharacterController))]
    public class PlayerController : MonoBehaviour {

    // Handling
    public float rotationSpeed = 450;
    public float walkSpeed = 5;
    public float runSpeed = 8;

    // System
    private Quaternion targetRotation;

    // Components
    private CharacterController controller;


    void Start () {
    controller = GetComponent<CharacterController>();
    }

    void Update (){
    Vector3 input = new Vector3(Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));

    if (Input != Vector3.zero) {
    targetRotation = Quaternion.LookRotation(input);
    transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y,targetRotation.eulerAngles.y,rotationSpeed * Time.deltaTime);
    }
    }
    }




    I'd appriciate it so much if some1 can help me. :D

    the link to the tuttorial is -
     

    Attached Files:

  21. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You should have made a new thread. This one is old, the last post is from Jan 2014.
    The problem in your code is that you typed 'Input' instead of input. 'Input' is a type, whereas 'input' is your variable that you most likely wanted to use.

    And please use Code Tags.
     
  22. adityaunnithan007

    adityaunnithan007

    Joined:
    Feb 7, 2016
    Posts:
    4
    Assets/Scripts/Player/PlayerMovement.cs(49,43): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
    whats this error all about please help me
     
  23. adityaunnithan007

    adityaunnithan007

    Joined:
    Feb 7, 2016
    Posts:
    4
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public float speed = 6f;

    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floorMask;
    float camRayLenght = 100f;

    void Awake()
    {
    floorMask = LayerMask.GetMask ("Floor");
    anim = GetComponent <Animator> ();
    playerRigidbody = GetComponent<Rigidbody> ();
    }

    void FixedUpdate()
    {
    float h = Input.GetAxisRaw ("Horizontal");
    float v = Input.GetAxisRaw ("Veritcal");

    Move (h, v);
    Turning ();
    Animating (h, v);

    }

    void Move (float h, float v)
    {
    movement.Set (h, 0f, v);

    movement = movement.normalized * speed * Time.deltaTime;

    playerRigidbody.MovePosition (transform.position + movement);
    }

    void Turning()
    {
    Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

    RaycastHit floorHit;

    if (Physics.Raycast (camRay, out floorHit, camRayLenght, floorMask))
    {
    Vector3 playerToMouse = floorHit.point - transform.position;
    playerToMouse.y = OffMeshLink;

    Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
    playerRigidbody.MoveRotation (newRotation);
    }
    }

    void Animating(float h, float v)
    {
    bool walking = h != 0f || v != 0f;
    anim.SetBool ("IsWalking", walking);
    }
    }
     
  24. adityaunnithan007

    adityaunnithan007

    Joined:
    Feb 7, 2016
    Posts:
    4
    this was my script ....i don't know why unity is showing error.
     
  25. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Please use code tags. And it's the best to always open a new thread, otherwise people who might wanna help read the other old stuff first and do not even get to your reply.

    This line is wrong:

    Code (CSharp):
    1.  playerToMouse.y = OffMeshLink;
    OffMeshLink is a class (a type). I don't know what's supposed to be assigned to the y-value of playerToMouse in "your" script, so i cannot offer any specific solution other than telling you it's supposed to a float (can also be an integer) that should be on the righthand side.
     
    adityaunnithan007 likes this.
  26. adityaunnithan007

    adityaunnithan007

    Joined:
    Feb 7, 2016
    Posts:
    4
    Thanks man , it worked. :D