0
\$\begingroup\$
using UnityEngine;
using System.Collections;

public class player_controler : MonoBehaviour {

    public float moveSpeed;
    public float jumpHeight;

    // Use this for initialization
    void Start () {
    }

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

        if(Input.GetKeyDown) (KeyCode.Space)) 
        {
            rigidbody2d.velocity = new vector2(0,jumpHeight;)
        }

    }
}

This is what shows up:

Assets/scripts/player_controler.cs(16,53): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

\$\endgroup\$
4
  • \$\begingroup\$ More information is needed. What is the error? Is this really all of the relevant code? \$\endgroup\$
    – Yecats
    Commented Jan 31, 2016 at 17:30
  • \$\begingroup\$ Shouldn't the semicolon be at the very end of the line? \$\endgroup\$
    – Peethor
    Commented Jan 31, 2016 at 17:31
  • \$\begingroup\$ this is what shows up. Assets/scripts/player_controler.cs(16,53): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement \$\endgroup\$
    – J kel
    Commented Jan 31, 2016 at 17:31
  • \$\begingroup\$ @Jkel I believe I see the problem and I posted the answer. If it works for you please do not forget to mark it as the correct answer. \$\endgroup\$
    – Yecats
    Commented Jan 31, 2016 at 17:40

3 Answers 3

1
\$\begingroup\$

Replace

    if(Input.GetKeyDown) (KeyCode.Space)) 
    {
        rigidbody2d.velocity = new vector2(0,jumpHeight;)
    }

with

    if(Input.GetKeyDown(KeyCode.Space)) 
    {
        rigidbody2d.velocity = new vector2(0,jumpHeight);
    }

It seems like you are missing some of the most basic concepts of C# syntax. Doing some basic C# tutorials (not necessarily Unity-specific) might help you to get a better feel for how the language is structured.

\$\endgroup\$
0
\$\begingroup\$

Is the semicolon after "jumpheight" just a typo, or is that really the problem. More information needed otherwise.

\$\endgroup\$
0
\$\begingroup\$

First, @peethor is correct in the comments. Your semi-colon is in the incorrect spot. (Should be at the end of the line. Also, your if statement syntax is incorrect.

Second, you need to get the component of the RigidBody2D before trying to set the velocity. (Make sure that the component exists on the same gameobject that this script is on. If not, you'll have to find the gameobject before getting the component.)

    public float moveSpeed;
    public float jumpHeight;
    private RigidBody2D rb;     
    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody2D>(); //Store a reference to it since you'll access it every update.
    }

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

        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = new vector2(0,jumpHeight);
        }
    }
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .