Search Unity

identifier expected error

Discussion in 'Scripting' started by rserocki, Mar 23, 2008.

Thread Status:
Not open for further replies.
  1. MABSUAREZ9

    MABSUAREZ9

    Joined:
    Aug 23, 2020
    Posts:
    1
    im getting an error that i cannot fix please help


    using UnityEngine;

    public class mouseLook : MonoBehaviour
    {
    public float mouseSensitivity = 100f;

    public Transform playerBody;

    float xRotation = 0f;


    // Start is called before the first frame update
    void Start()
    {
    Cursor. lockState = CurorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation. - 90f, 90f);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate(Vector3.up * mouseX);
    }
    }
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Use code tags and post the error.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Post a new thread, not a reply to an existing one
    Use code tags when you post code
    Copy and paste the actual error message
     
  4. Ninjab64

    Ninjab64

    Joined:
    Sep 23, 2020
    Posts:
    2
    i cannot fix this identifier expected error
    script:
    using System;
    using UnityEngine;
    using Random = UnityEngine.Random;

    namespace UnityStandardAssets.Characters.FirstPerson
    {
    [RequireComponent(typeof (CharacterController));]
    [RequireComponent(typeof (AudioSource))]
    public class FirstPersonController : MonoBehaviour
    {
    [SerializeField] private bool m_IsWalking;
    [SerializeField] private float m_WalkSpeed;
    [SerializeField] private float m_RunSpeed;
    [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    [SerializeField] private float m_JumpSpeed;
    [SerializeField] private float m_StickToGroundForce;
    [SerializeField] private float m_GravityMultiplier;
    [SerializeField] private MouseLook m_MouseLook;
    [SerializeField] private bool m_UseFovKick;
    [SerializeField] private FOVKick m_FovKick = new FOVKick();
    [SerializeField] private bool m_UseHeadBob;
    [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    [SerializeField] private float m_StepInterval;
    [SerializeField] private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
    [SerializeField] private AudioClip m_JumpSound; // the sound played when character leaves the ground.
    [SerializeField] private AudioClip m_LandSound; // the sound played when character touches back on ground.

    private Camera m_Camera;
    private bool m_Jump;
    private float m_YRotation;
    private Vector2 m_Input;
    private Vector3 m_MoveDir = Vector3.zero;
    private CharacterController m_CharacterController;
    private CollisionFlags m_CollisionFlags;
    private bool m_PreviouslyGrounded;
    private Vector3 m_OriginalCameraPosition;
    private float m_StepCycle;
    private float m_NextStep;
    private bool m_Jumping;
    private AudioSource m_AudioSource;

    // Use this for initialization
    private void Start()
    {
    m_CharacterController = GetComponent<CharacterController>();
    m_Camera = Camera.main;
    m_OriginalCameraPosition = m_Camera.transform.localPosition;
    m_FovKick.Setup(m_Camera);
    m_HeadBob.Setup(m_Camera, m_StepInterval);
    m_StepCycle = 0f;
    m_NextStep = m_StepCycle/2f;
    m_Jumping = false;
    m_AudioSource = GetComponent<AudioSource>();
    m_MouseLook.Init(transform , m_Camera.transform);
    }


    // Update is called once per frame
    private void Update()
    {
    RotateView();
    // the jump state needs to read here to make sure it is not missed
    if (!m_Jump)
    {
    m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    }

    if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    {
    StartCoroutine(m_JumpBob.DoBobCycle());
    PlayLandingSound();
    m_MoveDir.y = 0f;
    m_Jumping = false;
    }
    if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    {
    m_MoveDir.y = 0f;
    }

    m_PreviouslyGrounded = m_CharacterController.isGrounded;
    }


    private void PlayLandingSound()
    {
    m_AudioSource.clip = m_LandSound;
    m_AudioSource.Play();
    m_NextStep = m_StepCycle + .5f;
    }


    private void FixedUpdate()
    {
    float speed;
    GetInput(out speed);
    // always move along the camera forward as it is the direction that it being aimed at
    Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;

    // get a normal for the surface that is being touched to move along it
    RaycastHit hitInfo;
    Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
    desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;

    m_MoveDir.x = desiredMove.x*speed;
    m_MoveDir.z = desiredMove.z*speed;


    if (m_CharacterController.isGrounded)
    {
    m_MoveDir.y = -m_StickToGroundForce;

    if (m_Jump)
    {
    m_MoveDir.y = m_JumpSpeed;
    PlayJumpSound();
    m_Jump = false;
    m_Jumping = true;
    }
    }
    else
    {
    m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    }
    m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);

    ProgressStepCycle(speed);
    UpdateCameraPosition(speed);

    m_MouseLook.UpdateCursorLock();
    }


    private void PlayJumpSound()
    {
    m_AudioSource.clip = m_JumpSound;
    m_AudioSource.Play();
    }


    private void ProgressStepCycle(float speed)
    {
    if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    {
    m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    Time.fixedDeltaTime;
    }

    if (!(m_StepCycle > m_NextStep))
    {
    return;
    }

    m_NextStep = m_StepCycle + m_StepInterval;

    PlayFootStepAudio();
    }


    private void PlayFootStepAudio()
    {
    if (!m_CharacterController.isGrounded)
    {
    return;
    }
    // pick & play a random footstep sound from the array,
    // excluding sound at index 0
    int n = Random.Range(1, m_FootstepSounds.Length);
    m_AudioSource.clip = m_FootstepSounds[n];
    m_AudioSource.PlayOneShot(m_AudioSource.clip);
    // move picked sound to index 0 so it's not picked next time
    m_FootstepSounds[n] = m_FootstepSounds[0];
    m_FootstepSounds[0] = m_AudioSource.clip;
    }


    private void UpdateCameraPosition(float speed)
    {
    Vector3 newCameraPosition;
    if (!m_UseHeadBob)
    {
    return;
    }
    if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    {
    m_Camera.transform.localPosition =
    m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    newCameraPosition = m_Camera.transform.localPosition;
    newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    }
    else
    {
    newCameraPosition = m_Camera.transform.localPosition;
    newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    }
    m_Camera.transform.localPosition = newCameraPosition;
    }


    private void GetInput(out float speed)
    {
    // Read input
    float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    float vertical = CrossPlatformInputManager.GetAxis("Vertical");

    bool waswalking = m_IsWalking;

    #if !MOBILE_INPUT
    // On standalone builds, walk/run speed is modified by a key press.
    // keep track of whether or not the character is walking or running
    m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    #endif
    // set the desired speed to be walking or running
    speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    m_Input = new Vector2(horizontal, vertical);

    // normalize input if it exceeds 1 in combined length:
    if (m_Input.sqrMagnitude > 1)
    {
    m_Input.Normalize();
    }

    // handle speed change to give an fov kick
    // only if the player is going to a run, is running and the fovkick is to be used
    if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    {
    StopAllCoroutines();
    StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    }
    }


    private void RotateView()
    {
    m_MouseLook.LookRotation (transform, m_Camera.transform);
    }


    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
    Rigidbody body = hit.collider.attachedRigidbody;
    //dont move the rigidbody if the character is on top of it
    if (m_CollisionFlags == CollisionFlags.Below)
    {
    return;
    }

    if (body == null || body.isKinematic)
    {
    return;
    }
    body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    }
    }
    }
    error:
    Assets\Standard Assets\Characters\FirstPersonCharacter\Scripts\FirstPersonController.cs(7,52): error CS1001: Identifier expected
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Please read the literal comment directly above yours.
     
    JeffDUnity3D likes this.
  6. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Man, that sucks.
     
  7. Ozrolink6607

    Ozrolink6607

    Joined:
    Jun 13, 2021
    Posts:
    1
    I'm so confused it keeps giving me this error for this script.
     

    Attached Files:

  8. darkraider2882

    darkraider2882

    Joined:
    May 7, 2021
    Posts:
    1
    I'm having a similar problem with this code
    public class combat : MonoBehaviour
    {
    public Transform attackPoint;
    public float attackRange = 0.5f;
    // Update is called once per frame
    void Update()
    {
    Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
    foreach(Collider2D enemy in hitEnemies)
    {
    Debug.Log("We hit " + enemy.name);
    }
    void OnDrawGismosSelected();
    }

    If (attackPoint == null)
    return;
    Gizmos,DrawWireSphere(attackPoint.position, attackRange);
    }
    }
    it says that i need an identifier right before the ==
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Please stop replying to 13-year-old threads for help fixing your typos. In at least one place you're using a comma where it should be a period.

    Here is how to fix those kind of lazy fat-finger typos all on your own:

    Remember: NOBODY memorizes error codes. 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)

    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.

    ALSO, as long as you are monkey-pounding tutorials, how to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    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.

    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!
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'll just leave this here
     
    Kurt-Dekker likes this.
  11. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    If you came here from google or other search because you have this error, please read the thread. DO NOT create an new thread asking this question. It is a common and simple error that can be avoided by going through the learn section first, or reading the answers posted above.

    Closed.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.