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

Error CS0116, I was making a script to look around and this error came.HELP ME

Discussion in 'Scripting' started by Zombiegamer2244, Nov 16, 2021.

  1. Zombiegamer2244

    Zombiegamer2244

    Joined:
    Sep 8, 2021
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    private float AxisX;
    private float AxisY;

    public float Sensitivity;

    public class NewBehaviourScript1 : MonoBehaviour
    {
    // Start is called before the first frame update
    void Start()
    {
    Vector3 euler = transform.rotation.eulerAngles;
    X = euler.x;
    Y = euler.y;
    }

    // Update is called once per frame
    void Update()
    {
    const float MIN_X = 0.0f;
    const float MAX_X = 360.0f;
    const float MIN_Y = -90.0f;
    const float MAX_Y = 90.0f;

    AxisX += Input.GetAxis("Mouse X") * (Sensitivity * Time.deltaTime);
    if (AxisX < MIN_AxisX) AxisX += MAX_AxisX;
    else if (AxisX > MAX_AxisX) AxisX -= MAX_AxisX;
    AxisY -= Input.GetAxis("Mouse Y") * (Sensitivity * Time.deltaTime);
    if (AxisY < MIN_AxisY) AxisYY = MIN_AxisY;
    else if (AxisYY > MAX_AxisYY) Y = MAX_AxisYY;

    transform.rotation = Quaternion.Euler(Y, X, 0.0f);

    }
    }

    And i just copied someone elses look around script, there are 3 CS0116 errors, any ways to fix it?
     
  2. Shreddedcoconut

    Shreddedcoconut

    Joined:
    Jul 30, 2021
    Posts:
    61
    You shouldn't be creating floats or any variables outside of a class, e.g.
    Code (CSharp):
    1. private float AxisX;
    2. private float AxisY;
    3. public float Sensitivity;
    should be inside of your
    NewBehaviourScript1
    class, and not outside of it.

    Also, a simple google of the error code brings up lots and lots of answers to your question, for example Microsoft's documentation page for that specific error. And also an already-answered question about this error.

    Next time you make a post, please include the full error code AND message. I had to google the error code to figure out what it was because I didn't have the error message to go with it. Also remember to please put any code you post into a code block, using the Insert Code button.

    Also, copy-and-pasting code is OK sometimes, but you should try coding your own too, that way you know it won't have errors.
     
    Last edited: Nov 16, 2021
  3. Zombiegamer2244

    Zombiegamer2244

    Joined:
    Sep 8, 2021
    Posts:
    2
    YO thx bro, I am still a beginner. Learning to make more games :D,Thanks for the help on the code block thing. I will use ur thing THANKS!
     
    Shreddedcoconut likes this.