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

cs1002 error how do i fix?

Discussion in 'Scripting' started by comb11, Mar 10, 2021.

  1. comb11

    comb11

    Joined:
    Mar 10, 2021
    Posts:
    5
    i am very new to unity2d and i was watching tutorials on how to make a scrolling background and i copied this code and for some reason i get error cs1002 and i dont know what the problem is iv ben trying to find out for the past 2 hours i cant do it so maybe someone can help me.

    Here is my code





    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class backgroundscroller : MonoBehaviour
    {
    private BoxCollider2D boxcollider;
    private rigidbody2D rb;
    private float width;
    private float speed = -3f;
    // use this for initialization
    void Start()
    {
    boxcollider = getcomponent<boxcollider2D>();
    rb = getcomponent<rigidbody2D>();
    width = boxcollider.size.x
    rb.velocity = new vector2(speed, 0)
    }
    // Update is called once per frame
    void Update()
    {
    if (transform.position.x<-width)
    {
    reposition();
    }
    }
    private void reposition()
    {
    vector2 vector = new vector2(width * 2f, 0);
    transform.position = (vector2)transform.position + vector;
    }
    }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,787
    CS1002 is a missing semicolon.
    Please use code tags when showing code as its very hard to read otherwise https://forum.unity.com/threads/using-code-tags-properly.143875/

    You have a few problems with your script.
    They are easy to spot if you put them into a code editor such as Visual Studio
    The red lines indicate potential problems. Mouse over them for more details.
    upload_2021-3-10_16-41-56.png

    Code is case sensitive so you need to make sure to capitalize the classes, for example rigibody2D should be Rigidbody2D.

    getcomponent should be GetComponent

    You are also missing 2 semicolons,
    1 after each line here
    width = boxcollider.size.x
    rb.velocity = new vector2(speed, 0)

    This should be the end result
    Code (csharp):
    1.  
    2. using System.Collections;
    3.     using System.Collections.Generic;
    4.     using UnityEngine;
    5.  
    6.     public class backgroundscroller : MonoBehaviour
    7.     {
    8.         private BoxCollider2D boxcollider;
    9.         private Rigidbody2D rb;
    10.         private float width;
    11.         private float speed = -3f;
    12.         // use this for initialization
    13.         void Start()
    14.         {
    15.             boxcollider = GetComponent<BoxCollider2D>();
    16.             rb = GetComponent<Rigidbody2D>();
    17.             width = boxcollider.size.x;
    18.             rb.velocity = new Vector2(speed, 0);
    19.         }
    20.         // Update is called once per frame
    21.         void Update()
    22.         {
    23.             if (transform.position.x < -width)
    24.             {
    25.                 reposition();
    26.             }
    27.         }
    28.         private void reposition()
    29.         {
    30.             Vector2 vector = new Vector2(width * 2f, 0);
    31.             transform.position = (Vector2)transform.position + vector;
    32.         }
    33.     }
    34.  
     
    Joe-Censored and JeffDUnity3D like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    karl_jones and Joe-Censored like this.