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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I'm just learning Unity so maybe this is a stupid error, but I need help

Discussion in 'Scripting' started by unity_28FE7D34FD377C8932E5, Dec 1, 2022.

  1. unity_28FE7D34FD377C8932E5

    unity_28FE7D34FD377C8932E5

    Joined:
    Nov 30, 2022
    Posts:
    2
    As I said I'm just learning and I'm creating a simple platformer game, but I dunno why some errors appeared (I ran the game and it worked fine, but one second later the errors just appeared):

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour {
    2.     private Rigidbody2D rigidB;
    3.     private Animator animator;
    4.     private SpriteRenderer spriteR;
    5.  
    6.     float speed = 2f;
    7.     float xDirection;
    8.  
    9.     private void Start() {
    10.         rigidB = GetComponent<Rigidbody2D>();
    11.         animator = GetComponent<Animator>();
    12.         spriteR = GetComponent<SpriteRenderer>();
    13.     }
    14.  
    15.     private void Update() {
    16.         xDirection = Input.GetAxisRaw("Horizontal");
    17.         rigidB.velocity = new Vector2(xDirection * speed, rigidB.velocity.y);
    18.  
    19.         if(Input.GetButtonDown("Jump")) {
    20.             rigidB.velocity = new Vector2(rigidB.velocity.x, 6);
    21.         }
    22.  
    23.         UpdateAnimations();
    24.     }
    25.  
    26.     private void UpdateAnimations() {
    27.         if(Input.GetKey("x")) {
    28.             speed += 0.7f;
    29.         } else {
    30.             speed = 2f;
    31.         }
    32.  
    33.         if(speed > 7f) {
    34.             speed = 2.5f;
    35.         }
    36.  
    37.         if(xDirection > 0f) {
    38.             animator.SetBool("running", true);
    39.             spriteR.flipX = false;
    40.         } else if(xDirection < 0f) {
    41.             animator.SetBool("running", true);
    42.             spriteR.flipX = true;
    43.         } else {
    44.             animator.SetBool("running", false);
    45.         }
    46.     }
    47. }
    Code (CSharp):
    1. public class CameraController : MonoBehaviour {
    2.     [SerializeField] private Transform player;
    3.  
    4.     private void Update() {
    5.         transform.position = new Vector3(player.position.x, player.position.y, transform.position.z);
    6.     }
    7. }
    This are the only two scripts that I have and the errors are these:
    upload_2022-11-30_20-49-14.png E
     
  2. VadanaPon

    VadanaPon

    Joined:
    Jul 5, 2022
    Posts:
    45
    It looks like you have duplicated scripts, delete the duplicate and the problem should be solved.

    If you are using VS, if you change the name of the script in unity you need to close the script in VS and open it again, otherwise It will create a duplicate if you save

    Edit: it will also create a duplicate if you move it
     
    Last edited: Dec 1, 2022
  3. unity_28FE7D34FD377C8932E5

    unity_28FE7D34FD377C8932E5

    Joined:
    Nov 30, 2022
    Posts:
    2
    I'm looking in the scripts folder (I created it) and there're only the two scripts and the .meta. There aren't any duplicate
     
  4. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    stop writing private void
     
  5. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    That has absolutely nothing to do with it.
    Are you looking in the right folder because the error says the folder is Scripsts not Scripts. It is possible you have duplicate scripts elsewhere in your project structure.
     
  6. VadanaPon

    VadanaPon

    Joined:
    Jul 5, 2022
    Posts:
    45
    Forgot to add that moving the script (or folder) will also create a duplicate on saving
     
    Last edited: Dec 1, 2022
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please use the Getting Started or Scripting forums. Please only use the 2D forum when asking about 2D specific features etc. General C# scripting issues and typos or learning the language are not for this forum.

    I'll move your post to the scripting forum for you.

    Thanks.