Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Code pops up with error CS0246 and cant find the problem

Discussion in 'Scripting' started by TheRedCroissant, Jul 19, 2022.

  1. TheRedCroissant

    TheRedCroissant

    Joined:
    Jul 18, 2022
    Posts:
    7
    Cant find where I went wrong, just started using unity, if anyone could explain it a slight bit or help me with it, would help a ton!


    The full error msg is - Assets\PlayerMovement.cs(17,6): error CS0246: The type or namespace name 'RigidBody' could not be found (are you missing a using directive or an assembly reference?) Code should be attached below this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.      [Header("Movement")]
    8.      public float moveSpeed;
    9.  
    10.      public Transform orientation;
    11.  
    12.      float horizontalInput;
    13.      float verticalInput;
    14.  
    15.      Vector3 moveDirection;
    16.  
    17.      RigidBody rb;
    18.  
    19.      private void start() {
    20.          rb = GetComponent<RigidBody>();
    21.          rb.freezerotation = true;
    22.      }
    23.    
    24.      private void update() {
    25.          MyInput();
    26.      }
    27.      private void FixedUpdate() {
    28.          Moveplayer();
    29.      }
    30.    
    31.    
    32.      private void MyInput() {
    33.          horizontalInput = Input.GetAxisRaw("Horizontal");
    34.          verticalInput = Input.GetAxisRaw("Vertical");
    35.      }
    36.  
    37.        private void MovePlayer() {
    38.            moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
    39.  
    40.            rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
    41.        }
    42.  
    43.  
    44.  
    45. }
    46.  
     
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    Because it´s written with a lower case b: Rigidbody.
    A short look in the unity doc or using the auto-complition of the IDE would have solved that problem easily
     
    Bunny83 and Kurt-Dekker like this.
  3. TheRedCroissant

    TheRedCroissant

    Joined:
    Jul 18, 2022
    Posts:
    7
    leads to this
    Assets\PlayerMovement.cs(21,13): error CS1061: 'Rigidbody' does not contain a definition for 'freezerotation' and no accessible extension method 'freezerotation' accepting a first argument of type 'Rigidbody' could be found (are you missing a using directive or an assembly reference?
     
  4. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    As before the error message says exaclty what the problem is: 'Rigidbody' does not contain a definition for 'freezerotation'' and again a short look in the docu or using auto-complition would solve the problem in seconds.

    There is no method "freezerotation". It´s "freezeRotation"...
     
    Bunny83 likes this.
  5. TheRedCroissant

    TheRedCroissant

    Joined:
    Jul 18, 2022
    Posts:
    7
    Thank you, it helped with part of the code, now this error appears in the console
    Assets\PlayerMovement.cs(28,10): error CS0103: The name 'Moveplayer' does not exist in the current context
     
  6. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    106
    The error points to line 28. Go to that line and you should quickly see the problem (hint: you've made the same exact mistake that led to your other two errors).
     
    mopthrow likes this.
  7. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Function/Method names like MovePlayer() are PascalCase, field names like freezeRotation are camelCase for future reference. Should make it easier to fix these ;)

    At a glance, you also need to capitalise your Update() function or that won't run every frame as you expect. Your Start() will not run at the start unless you capitalise that too.
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Are you following a tutorial? If not, you should be! You also have the "update" and "start" methods with the same issue as your other 3, you are not capitalizing your code correctly. Those won't show an error, but they won't be called correctly. This and THIS are different in C#
     
    Bunny83, Chubzdoomer and mopthrow like this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    STOP, you are doing this tutorial business all wrong. It's going to take you forever and be extremely frustrating with your approach, and with your approach, you will learn NOTHING. Instead, try this approach:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you make simple typing mistakes, don't post here, you can fix it yourself.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  10. TheRedCroissant

    TheRedCroissant

    Joined:
    Jul 18, 2022
    Posts:
    7
    Thanks for helping me get that, forgot that C# was case sensetive, ty!
     
  11. TheRedCroissant

    TheRedCroissant

    Joined:
    Jul 18, 2022
    Posts:
    7
    Thanks again Kurt, Im gonna just copy this down now