Search Unity

identifier expected error

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

  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#.
     
  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...
     
    arisczdrck and Nanako like this.
  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
     
  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,466
    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,466
  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:
    7,051
    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:
    7,173
    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:
    400
    You have a semicolon after MonoBehaviour. Also, system should be System in line 2.
     
  33. StarManta

    StarManta

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