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. Dismiss Notice

Got the Error "void cannot be used in this context"

Discussion in 'Scripting' started by agood150, Oct 11, 2014.

  1. agood150

    agood150

    Joined:
    Oct 11, 2014
    Posts:
    2
    Hello Guys, iam making a 2D platformer right now, all work ive done yet. But now I added jumping and now there is comming this Error if i want test?!

    Can you help me Guys, here is the whole script and a pic.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.     public float gravity = 8;
    6.     public float speed = 10;
    7.     public float JumpPower = 10;
    8.    
    9.     bool inputJump = false;
    10.     float velocity = 0;
    11.     Vector3 moveDirection = Vector3.zero;
    12.     CharacterController characterController;
    13.     // Use this for initialization
    14.     void Start () {
    15.         characterController = GetComponent<CharacterController> ();
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         InputCheck ();
    22.         Move ();
    23.         SetAnimation ();
    24.     }
    25.  
    26.     void InputCheck()
    27.     {
    28.         velocity = Input.GetAxis ("Horizontal") * speed;
    29.  
    30.         if (Input.GetKeyDown (KeyCode.Space))
    31.         {
    32.             inputJump = true;
    33.         }
    34.         else
    35.         {
    36.             inputJump = false;
    37.     }
    38.  
    39.     void Move()
    40.     {
    41.        
    42.         if (characterController.isGrounded)
    43.         {
    44.             if (inputJump)
    45.                 moveDirection.y = jumpPower;
    46.         }
    47.         else
    48.         {
    49.             moveDirection.y -= gravity *Time.deltaTime;
    50.         }
    51.  
    52.         moveDirection.x = velocity;
    53.         moveDirection.y -= gravity;
    54.         characterController.Move (moveDirection * Time.deltaTime);
    55.  
    56.     }
    57.  
    58.     void SetAnimation()
    59.     {}
    60. }
    61.  
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. void InputCheck()
    2.     {
    3.         velocity = Input.GetAxis ("Horizontal") * speed;
    4.         if (Input.GetKeyDown (KeyCode.Space))
    5.         {
    6.             inputJump = true;
    7.         }
    8.         else
    9.         {
    10.             inputJump = false;
    11.     }
    You forgot a bracket, it's supposed to be like this:
    Code (CSharp):
    1. void InputCheck()
    2.     {
    3.         velocity = Input.GetAxis ("Horizontal") * speed;
    4.         if (Input.GetKeyDown (KeyCode.Space))
    5.         {
    6.             inputJump = true;
    7.         }
    8.         else
    9.         {
    10.             inputJump = false;
    11.         }
    12.     }
     
    agood150 likes this.
  3. agood150

    agood150

    Joined:
    Oct 11, 2014
    Posts:
    2
    OMG Thank you so much, now it works ^^
     
  4. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Can't tell you how many times I have done this myself.
    Spent most of a day looking at scripts before only to find out in the end all it was, was one } missing.

    Good luck