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 22, 2014.

  1. hammy2747

    hammy2747

    Joined:
    Apr 19, 2014
    Posts:
    19
    I have had this error before, but it was a simple mistake. This time i cannot find any errors in my code, but it says that there are some there...
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HenryControlsScript : MonoBehaviour
    5. {
    6.     public float maxSpeed = 20f;
    7.     bool facingRight = true;
    8.  
    9.     Animator anim;
    10.  
    11.     bool grounded = false;
    12.     public Transform groundCheck;
    13.     float groundRadius = .2f;
    14.     public LayerMask whatisGround;
    15.     public float jumpForce = 700f;
    16.  
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         anim = GetComponent<Animator>();
    22.     }
    23.     // Update is called once per frame
    24.     void FixedUpdate ()
    25.     {
    26.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatisGround);
    27.         anim.SetBool ("Ground", grounded);
    28.  
    29.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    30.        
    31.         float move = Input.GetAxis ("Horizontal");
    32.         anim.SetFloat("Speed", Mathf.Abs(move));
    33.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    34.         if (move > 0  !facingRight) {
    35.            
    36.             Flip ();
    37.            
    38.         }
    39.         else if (move < 0  facingRight) {
    40.            
    41.             Flip ();
    42.         }
    43.         void Update()     // it says that this 'void' here is not usable
    44.         {
    45.             if (grounded  Input.GetKeyDown(KeyCode.Space))
    46.             {
    47.                 anim.SetBool("Ground" = false);
    48.                 rigidbody2D.AddForce(new Vector2(0,jumpForce));
    49.             }
    50.         }
    51.     }
    52.     void Flip ()
    53.     {
    54.         facingRight = !facingRight;                    
    55.         Vector3 theScale = transform.localScale;      
    56.         theScale.x *=-1;
    57.         transform.localScale = theScale;
    58.     }
    59. }
    Thanks for anyone's help.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You have another function inside of a function

    This time it's Update inside of FixedUpdate.
     
  3. hammy2747

    hammy2747

    Joined:
    Apr 19, 2014
    Posts:
    19
    Ok, so how can i fix this?
    Sorry, im kinda new to c#
     
  4. hammy2747

    hammy2747

    Joined:
    Apr 19, 2014
    Posts:
    19
    nevermind, i just fixed it