Search Unity

identifier expected error

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

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

    rserocki

    Joined:
    Oct 25, 2007
    Posts:
    24
    Three keys send messages to an object simulating a plunger. They may lower or raise it and tell it to 'release' the ball.

    I've had trouble with the release part. I had been doing the input in FixedUpdate but when getting the plunger object (which is a block) to move quickly up to its original position, it would either zip through the ball or else cause Unity to grind to a halt, depending on how I did this.

    So I decided to go by a .cs example and make a public class for the plunger. I wanted to put the control responses in FixedUpdate and I decided to have a OnTriggerStay as a separate function within the class. I wanted to have OnTriggerStay contain a conditional stated elsewhere in the class so that the ball would be propelled by the trigger function only if the release key were hit.

    However, I'm getting an Identifier expected error and I don't know what I'm doing wrong. I am informed by online resources that this happens in C# when a reserved word is used for a variable, but I can't see what I'm doing wrong.

    Thanks in advance.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6. public class PlungerController : MonoBehaviour
    7. {
    8.         public float atRestY = -2.9F;
    9.         public float maxPullY = -2.96F;
    10.         public int hasReleased = 0;
    11.         public float distanceY = 0;
    12.  
    13.  
    14.         void FixedUpdate ()
    15.         {
    16.                  
    17.         if      (Input.GetButton("PlungerDown")) {              
    18.                 rigidbody.isKinematic = true;
    19.  
    20.                 if (distanceY > -384) {
    21.                         distanceY--;
    22.                     transform.Translate(Vector3.up * -1 * Time.deltaTime);
    23.              }
    24.                          
    25.         }
    26.  
    27.         if      (Input.GetButton("PlungerUp")) {                
    28.                 rigidbody.isKinematic = true;
    29.  
    30.                 if (transform.position.y < atRestY) {
    31.                         distanceY++;
    32.                    transform.Translate(Vector3.up * Time.deltaTime);
    33.                    }  
    34.          }
    35.  
    36.         if (Input.GetButton("PlungerRelease")) {
    37.                      hasReleased = 1;
    38.          }
    39.                  
    40.     } // end of FixedUpdate
    41.      
    42.    function OnTriggerStay (other : Collider) {
    43.          if (hasReleased  == 1) {
    44.         if (other.attachedRigidbody) {
    45.            other.attachedRigidbody.AddForce(Vector3.up * 10);
    46.          }
    47.       }
    48.  
    49.    }
    50. }
    51.  
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    You wrote 'function OnTriggerStay (other : Collider)', which is Javascript syntax. That needs to be 'void OnTriggerStay(Collider other)' for C#.
     
    Bunny83 likes this.
  3. rserocki

    rserocki

    Joined:
    Oct 25, 2007
    Posts:
    24
    Thanks very much, NCarter.
     
  4. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
    I got the same error with this code
    public class GameManger : MonoBehaviour {
     
  5. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
    please help
     
  6. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    You're going to have to post more of your script than that.

    Unless that's all you've got...
     
  7. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
    ok one sec
     
  8. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
    full scripe
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic

    public class GameManger : MonoBehaviour {
    public List<Character> Characters = new List<Character>();
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    }
    [System.Serializable]
    public class Character
    {
    public string Name;
    public Texture2D Icon;
    public GameObject playerprefab;
    }
     
  9. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
    that is the full one
     
  10. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
    The problem

    public class GameManger : MonoBehaviour {
     
  11. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    You're missing a semi-colon from this line:

    Code (CSharp):
    1. using System.Collections.Generic
     
    raajcm1 likes this.
  12. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
  13. tfishy101

    tfishy101

    Joined:
    Dec 3, 2014
    Posts:
    8
    never mind thanks so much
     
  14. Evamushroomeva

    Evamushroomeva

    Joined:
    Nov 24, 2015
    Posts:
    4
    I'm having a similar issue with one of my lines of code (functionOnCollisionEnter(collision; Collision)) and I'm not sure what I'm doing wrong:

    usingUnityEngine;
    usingSystem.Collections;

    publicclassIfCaught : MonoBehaviour {

    //Usethisforinitialization
    functionStart () {

    audio = GetComponent<AudtioSource> ;

    }

    //Updateiscalledonceperframe
    functionUpdate () {

    }

    functionOnCollisionEnter(collision; Collision) {
    //dostuffwithcollision
    if(collision.relativeVelocity.magnitude > 2) {
    audio.Play();
    }
    }
    }
     
  15. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You are using a semicolon when you should be using a colon.

    Code (csharp):
    1. function OnCollisionEnter(collision : Collision) {
     
  16. Evamushroomeva

    Evamushroomeva

    Joined:
    Nov 24, 2015
    Posts:
    4
    I fixed it, but it's still giving me an error

    usingUnityEngine;
    usingSystem.Collections;

    publicclassIfCaught : MonoBehaviour {

    //Usethisforinitialization
    functionStart () {

    audio = GetComponent<AudtioSource> ;

    }

    //Updateiscalledonceperframe
    functionUpdate () {

    }

    functionOnCollisionEnter(collision : Collision) {
    //dostuffwithcollision
    if(collision.relativeVelocity.magnitude > 2) {
    audio.Play();
    }
    }
    }
     
  17. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Use code tags to preserve formatting. Tell us the error message so we can easily find the problem.

    I assume it is this line:
    Code (csharp):
    1. audio = GetComponent<AudtioSource> ;
    Should be
    Code (csharp):
    1. audio = GetComponent.<AudioSource> ();
    I recommend you go through some tutorials if you haven't already.
    http://unity3d.com/learn
     
    Last edited: Dec 15, 2015
  18. Evamushroomeva

    Evamushroomeva

    Joined:
    Nov 24, 2015
    Posts:
    4
    the error code I'm getting is:

    Assets/Standard Assets/Characters/FirstPersonalCharacter/Scripts/IfCaught.cs(19,36); error CS1041: Identifier expected

    My code is now:

    usingUnityEngine;
    usingSystem.Collections;


    publicclassIfCaught : MonoBehaviour {

    //Usethisforinitialization
    functionStart () {

    audio = GetComponent<AudioSource> () ;

    }

    //Updateiscalledonceperframe
    functionUpdate () {

    }

    functionOnCollisionEnter (collision : Collision) {
    //dostuffwithcollision
    if(collision.relativeVelocity.magnitude > 2) {
    audio.Play();
    }
    }
    }
     
  19. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're mixing JS syntax and C# syntax.
     
    DanielQuick likes this.
  20. Evamushroomeva

    Evamushroomeva

    Joined:
    Nov 24, 2015
    Posts:
    4
    what should/would I do to fix it?

    I'm sorry, I'm really bad at this.
     
  21. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  22. MaxRCG

    MaxRCG

    Joined:
    Mar 25, 2017
    Posts:
    2
    I dont get what the error is in this script (the horizontal and vertical axis comment should be ignored, unless its causing the error.) Im writing this from Page 36 of Unity In Action by Joseph Hocking.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseLook : MonoBehaviour {
    5.     public enum RotationAxes {
    6.         MouseXAndY = 0,
    7.         MouseX = 1,
    8.         MouseY = 2,
    9.     }
    10.     public RotationAxes axes = RotationAxes.MouseXAndY;
    11.  
    12.     public float sensitivityHor = 9.0f;
    13.     public float sensitivityVert = 9.0f;
    14.  
    15.     public float minimumVert = -45.0f;
    16.     public float maximumVert = 45.0f;
    17.  
    18.     private float _rotationX = 0;
    19.  
    20.     void Update () { //ERROR HERE : " Identifier Expected "
    21.         if (axes == RotationAxes.MouseX) {
    22.             transform.Rotate (0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
    23.         }
    24.         else if (axes == RotationAxes.MouseY) {
    25.             _rotationX -= Input.GetAxis ("Mouse Y") * sensitivityVert;
    26.             _rotationX = Mathf.Clamp (_rotationX, minimumVert, maximumVert);
    27.  
    28.             float rotationY = transform.localEulerAngles.y;
    29.  
    30.             transform.localEulerAngles = new Vector3 (_rotationX, rotationY, 0);
    31.         }
    32.         else {
    33.             // both horizontal and vertical rotation here
    34.     }
    35. } // ERROR HERE: " } expected "
    36.  
     
  23. MaxRCG

    MaxRCG

    Joined:
    Mar 25, 2017
    Posts:
    2
    Nevermind, needed a extra curly brace, thought it wasn't seeing the } at the end, while it just needed another one.
     
  24. metavoidthedarkassassin

    metavoidthedarkassassin

    Joined:
    May 6, 2018
    Posts:
    1
    im getting same error here`s mine
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class Reset : MonoBehaviour
    6. {
    7.     private void If(Input.GetKeyDown(KeyCode.R));
    8.     {
    9.         Debug.Log("I'm being called");
    10.         SceneManager.LoadScene(0);
    11.     }
    12. }
    13.  
     
  25. saiarjun546

    saiarjun546

    Joined:
    Sep 30, 2019
    Posts:
    4
    I'm getting an 'identifier expected' error when I'm using the vector3 variable.

    This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(PlayerMotor))]
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     [Serealizedfield]
    7.     private float speed = 5f;
    8.  
    9.     private PlayerMotor motor;
    10.  
    11.     void start()
    12.     {
    13.         motor = GetComponent<PlayerMotor>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         float xMov = Input.GetAxisRaw("Horizontal");
    19.         float zMov = Input.GetAxisRaw("Vertical");
    20.  
    21.         Vector3 _movHorizontal = transform.right * _xMov;
    22.         Vector3 _movVertical = transform.forward * _zMov;
    23.  
    24.         Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
    25.  
    26.         motor Move (_velocity);
    27.     }
    28. }
     
  26. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You need a dot in between "motor" and "Move".
     
  27. saiarjun546

    saiarjun546

    Joined:
    Sep 30, 2019
    Posts:
    4
    it is saying 'identifier expected' in the last statement after(_velocity)
     
  28. saiarjun546

    saiarjun546

    Joined:
    Sep 30, 2019
    Posts:
    4
    Thanks a lot StarManta for your timely help.
     
  29. saiarjun546

    saiarjun546

    Joined:
    Sep 30, 2019
    Posts:
    4
    Unity is still giving the statement 'All compiler errors have to be fixed before entering PlayMode'
     
  30. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well you'll have to fix every error. The console is telling you where every single error is. Your code editor should be doing so as well. If you're stumped by another one, you can post it here again.
     
  31. tobias3342

    tobias3342

    Joined:
    Jan 8, 2020
    Posts:
    1
    hey anyone help me with this im getting the error cs1001 and i don't see anything wrong with my script
    script:
    using.Collections;
    using system.Collections.Generic;
    using UnityEngine;
    public class Move2d : MonoBehaviour; {
    public float movespeed = 5f;
    void start () {

    }
    void Update () {
    Vector3 movement = new Vector3(Input.Getaxis("horizontal"), 0f, 0f);
    transform.position += movement * Time.deltaTime * movespeed;

    }
    }
     
  32. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    You have a semicolon after MonoBehaviour. Also, system should be System in line 2.
     
  33. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Also, use code tags when posting code, and post your questions in their own thread rather than necro'ing an old thread.
     
  34. ssundeev5

    ssundeev5

    Joined:
    Feb 15, 2020
    Posts:
    1
    im getting the same error, here is the code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

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

    public Transform playerBody;

    float xRotation = 0f;

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

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

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

    tansform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    playerBody.Rotate.(Vector3.up * mouseX);



    }
    }
     
  35. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Literally the comment immediately above yours, and you did the exact opposite of both things.
     
  36. killzone31max

    killzone31max

    Joined:
    Mar 30, 2020
    Posts:
    1
    my code is not working. its my first time coding , i dont know what im doing wrong

    this is my code

    using UnityEngine;

    public class playercollision : MonoBehaviour {

    public PlayerMovement movement;

    void OnCollisionEnter (CollisionInfo) //CS1001


    {
    if(collisionInfo.collider.tag="Obstacle")
    {
    Debug.Log(collisionInfo.collider.name="Obstacle");
    }

    }


    thanks
    }
     
  37. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Oh my god how do you do the exact same two wrong things on the exact same thread as BOTH people immediately above you?
     
    Joe-Censored likes this.
  38. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You need to specified a parameter's type when declaring a method. See how it is written in the example.

    https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

    Also, capitalization matters in C#. You have CollisionInfo written with two different capitalizations, so the compiler will see those as completely separate variables. And I have no idea what you're doing with that assignment inside of the Debug.Log statement. It is nonsense.

    I'm convinced first time posters rarely read past the title of threads they necro. Just find somewhat relevant thread, and post without reading.
     
  39. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
    can someone please help me with this error:

    Assets\Scripts\GameManager.cs(60,40): error CS1001: identifier expected


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class GameManager : MonoBehaviour{

    public delegate void GameDelegate();
    public static event GameDelegate OnGameStarted;
    public static event GameDelegate OnGameOverConfirmed;


    public static GameManager Instance;

    public GameObject StartPage;
    public GameObject GameOverPage;
    public GameObject CountdownPage;
    public Text ScoreText;

    enum PageState {
    None,
    Start,
    GameOver,
    Countdown
    }

    int score = 0;
    bool gameOver = false;

    public bool GameOver {get{return gameOver; } }

    void Awake() {
    Instance = this;
    }

    void OnEnable() {
    CountdownText.OnCountdownFinished += OnCountdownFinished;
    TapController.OnPlayerDied += OnPlayerDied;
    TapController.OnPlayerScored += OnPlayerScored;
    }

    void OnDisable() {
    CountdownText.OnCountdownFinished -= OnCountdownFinished;
    TapController.OnPlayerDied -= OnPlayerDied;
    TapController.OnPlayerScored -= OnPlayerScored;
    }

    void OnCountdownFinished() {
    SetPageState(PageState.None);
    OnGameStarted();
    score = 0;
    GameOver = false;
    }

    void OnPlayerDied() {
    GameOver = true;
    int savedScore = PlayerPrefs.GetInt("HighScore");
    if (score > savedScore) NewMethod();
    PlayerPrefs.SetInt("HighScore", score);
    }
    //60-------------------------------------------------------> SetPageState(PageState.GameOver);
    }

    void OnPlayerScored() {
    score++;
    ScoreText.text = score.ToString();
    }
    void SetPageState(PageState state) {
    switch (state) {
    case PageState.None:
    StartPage.SetActive(false);
    GameOverPage.SetActive(false);
    CountdownPage.SetActive(false);
    break;
    case PageState.Start:
    StartPage.SetActive(true);
    GameOverPage.SetActive(false);
    CountdownPage.SetActive(false);
    break;
    case PageState.GameOver:
    StartPage.SetActive(false);
    GameOverPage.SetActive(true);
    CountdownPage.SetActive(false);
    break;
    case PageState.Countdown:
    StartPage.SetActive(false);
    GameOverPage.SetActive(false);
    CountdownPage.SetActive(true);
    break;
    }
    }

    public void ConfirmGameOver() {
    //activated when replay button is hit
    //OnGameOverConfirmed(); //event
    ScoreText.text = "0";
    SetPageState(PageState.Start);
    }

    public void StartGame() {
    //activated when play button is hit
    SetPageState(PageState.Countdown);
    }
     
  40. Mackerel67

    Mackerel67

    Joined:
    May 6, 2020
    Posts:
    11
  41. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Ways not to use this forum: this ^
    If you'd bothered reading the thread, you'd see that three people directly above you have ALREADY been told off for doing exactly the same thing you then proceeded to do.

    1) If you have a question, post a new thread. Don't hijack an old thread just because it came up on your search results.
    2) Use code tags. They make your code readable and give line numbers, which is very important for finding the error in a long script file like this one.
     
  42. waleedkpp

    waleedkpp

    Joined:
    May 14, 2020
    Posts:
    4
    My error(s) : Assets\Scripts\EndTrigger.cs(9,21): error CS1001: Identifier expected ,
    EAssets\Scripts\EndTrigger.cs(9,21): error CS1002: ; expected
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EndTrigger : MonoBehaviour{
    4.  
    5.     public GameManager gameManager;
    6.  
    7.     void OnTriggerEnter ()
    8.     {
    9.         gameManager.completeLevel();
    10.     }
    11.  
    12. }
    13.  
     
  43. waleedkpp

    waleedkpp

    Joined:
    May 14, 2020
    Posts:
    4
    pls
    help
     
  44. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    You must be trolling.
     
  45. waleedkpp

    waleedkpp

    Joined:
    May 14, 2020
    Posts:
    4
    what?
     
  46. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    You post a question in a 12 year old thread where four other people have been told not to do that.

    You used code tags, though.
     
  47. waleedkpp

    waleedkpp

    Joined:
    May 14, 2020
    Posts:
    4
    oh sry
     
  48. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    At this point posting yet another new question in this thread ought to be just an insta-ban (at least a temporary insta-ban). There could be no clearer evidence that someone is too lazy to read a single thing to help themselves and is just expecting the forum to do the work for them.
     
    Boz0r likes this.
  49. supergamers_

    supergamers_

    Joined:
    Jul 14, 2020
    Posts:
    1
    Hi, I got the same error with the following code, as I was switching scenes.
     

    Attached Files:

  50. Please
    1; open your own thread.
    2; include your code inside your post in CODE tags, do not upload it as attachments
     
Thread Status:
Not open for further replies.