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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Keyword 'void' cannot be used in this context

Discussion in '2D' started by hammy2747, Apr 20, 2014.

  1. hammy2747

    hammy2747

    Joined:
    Apr 19, 2014
    Posts:
    19
    I have looked everywhere, but i cannot find out how to fix this
    Here is my code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HenryControlsScript : MonoBehaviour {
    5.  
    6.     public float maxSpeed = 10f;
    7.     bool facingRight = true;
    8.    
    9.     Animator anim;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         anim = GetComponent<Animator>();
    14.     }
    15.     // Update is called once per frame
    16.     void FixedUpdate () {
    17.         float move = Input.GetAxis ("Horizontal");
    18.        
    19.         anim.SetFloat("Speed", Mathf.Abs(move));
    20.        
    21.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    22.        
    23.         if (move > 0  !facingRight)
    24.             Flip ();
    25.         else if (move < 0  facingRight) {
    26.             Flip ();
    27.         };
    28.         void Flip () {
    29.             facingRight = !facingRight;                    
    30.             Vector3 theScale = transform.localScale;      
    31.             theScale.x *=-1;
    32.             transform.localScale = theScale;
    33.         }
    34.     }
    35.  
    When i try to run this, there are my errors:
    Assets/Scripts/HenryContAssets/Scripts/HenryControlsScript.cs(28,27): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
    Assets/Scripts/HenryControlsScript.cs(28,25): error CS1547: Keyword `void' cannot be used in this context
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    On line 27
    Code (csharp):
    1.  
    2.         };
    3.  
    Remove the ;
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually, the error is that you can't put functions inside other functions. The }; thing won't cause any errors (but should be removed anyway since it doesn't do anything).

    --Eric
     
  4. wondyr

    wondyr

    Joined:
    Nov 30, 2013
    Posts:
    36
    A few errors I can see is the ; after the } . You are also missing some {} as well.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HenryControlsScript : MonoBehaviour
    5. {
    6.     public float maxSpeed = 10f;
    7.     bool facingRight = true;
    8.     Animator anim;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         anim = GetComponent<Animator>();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void FixedUpdate ()
    18.     {
    19.         float move = Input.GetAxis ("Horizontal");
    20.         anim.SetFloat("Speed", Mathf.Abs(move));
    21.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    22.         if (move > 0  !facingRight)
    23.         {//added
    24.             Flip ();
    25.         }//added
    26.         else if (move < 0  facingRight)
    27.         {
    28.             Flip ();
    29.         }//removed ;
    30.     }//added   
    31.         void Flip ()
    32.         {
    33.             facingRight = !facingRight;                    
    34.             Vector3 theScale = transform.localScale;      
    35.             theScale.x *=-1;
    36.             transform.localScale = theScale;
    37.            
    38.         }
    39.        
    40. }
    41.  
     
  5. playmonkey1

    playmonkey1

    Joined:
    Sep 20, 2013
    Posts:
    60
    @wondyr is correct tho for for single statements after an If..Else you don't really need curly brackets
     
    Last edited: Apr 20, 2014
  6. hammy2747

    hammy2747

    Joined:
    Apr 19, 2014
    Posts:
    19
    Sorry for the nooby question, but would this work?
    Code (csharp):
    1. if (move > 0  !facingRight) {
    2.                         Flip ();
    3.        }
    4.             else if (move < 0  facingRight) {
    5.                     Flip ();
    6.             }
     
  7. hammy2747

    hammy2747

    Joined:
    Apr 19, 2014
    Posts:
    19
    And whenever I try to add the } for the public class HenryControlScript : MonoBehaviour
    i get a parsing error, but i have a feeling that when i fix the 'keyword void cannot...' error that will fix it all
     
    Last edited: Apr 20, 2014
  8. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    wondyr has already posted a corrected version of your code four posts above.. I think you should spend more time learning the language because this is really trivial and fixing it should be self-evident for anyone with the most basic knowledge of C# or any C-derived language.
     
  9. hammy2747

    hammy2747

    Joined:
    Apr 19, 2014
    Posts:
    19
    ok thanks guys, but one more problem now that i have that working, my animations are not playing at the right time...

    and it is giving me the error'Main Camera' AnimationEvent has no function name specified
    and
    Animator has not been initialized (2)
     
    Last edited: Apr 20, 2014
  10. maanamar

    maanamar

    Joined:
    Jun 19, 2017
    Posts:
    1
    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    [Header("Movement")]
    public float movementSpeed;
    public float Velocity;
    public Rigidbody rb;
    public Animator anim;

    [Header("Combat")]
    private bool attacking;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    GetInput();
    Move ();
    }

    void GetInput(){
    //Attack
    if(Input.GetMouseButtonDown(0)){
    print("Attacking");
    Attack();
    }

    //move left
    if(Input.GetKey(KeyCode.A)){
    SetVelocity(-1);
    }
    else if(Input.GetKeyUp(KeyCode.A)){
    SetVelocity(0);
    anim.SetInteger("Condition", 0);
    }
    //move right
    if(Input.GetKey(KeyCode.D)){
    SetVelocity(1);
    }
    else if(Input.GetKeyUp(KeyCode.D)){
    SetVelocity(0);
    anim.SetInteger("Condition", 0);
    }
    }

    void Move(){
    if(Velocity == 0){
    anim.SetInteger("Condition", 0);
    return;
    } else {
    if(!attacking){
    anim.SetInteger("Condition", 1);
    }

    rb.MovePosition(transform.position + (Vector3.right * Velocity * movementSpeed * Time.deltaTime));
    }

    void SetVelocity(float dir){
    //look left or right depending on the (- +) of dir.
    if(dir < 0)transform.LookAt(transform.position + Vector3.left);
    else if(dir > 0)transform.LookAt(transform.position + Vector3.right);
    velocity = dir;
    }

    void Attack(){
    if(attacking) return;
    anim.SetInteger("Condition", 2);
    StartCoroutine(AttackRoutine);
    }

    IEnumerator AttackRoutine(){
    attacking = true;
    yield return new WaitForSeconds (1);
    attacking = false;

    }
    }