Error CS1612 - Issue with rigidbody

Hi everyone, I’m learning unity with C# and I’ve been going through a tutorial on youtube (http://youtu.be/hCysoAoV3dc). He’s using Javascript for his tutorial in which I’m not interested because I prefer learning C#. His code works in Javascript, but for some reason I can’t have it to work in C# as soon as it gets to rigidbody… It gives me these following errors:

Assets/PlayerControls.cs(13,37): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity’. Consider storing the value in a temporary variable

Assets/PlayerControls.cs(17,37): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity’. Consider storing the value in a temporary variable

Assets/PlayerControls.cs(21,37): error CS1612: Cannot modify a value type return value of `UnityEngine.Rigidbody2D.velocity’. Consider storing the value in a temporary variable

Finally, here’s my code:

using UnityEngine;
using System.Collections;

public class PlayerControls : MonoBehaviour 
{
	public KeyCode moveUp, moveDown;
	public float speed = 10;

	void Update () 
	{
		if (Input.GetKey(moveUp))
		{
			rigidbody2D.velocity.y = speed;
		}
		else if (Input.GetKey(moveDown))
		{
			rigidbody2D.velocity.y = speed * -1;
		}
		else
		{
			rigidbody2D.velocity.y = 0;
		}
	}
}

EDIT: I realize somehow that this was put in the wrong section… It was suppose to go under Forum → Unity Community Support → Scripting.

Try this, and make sure you have added Rigidbody2D component to your model :

using UnityEngine;

using System.Collections;

 

public class PlayerControls : MonoBehaviour 

{

    public KeyCode moveUp, moveDown;

    public float speed = 10;

    Rigidbody2D rigidbody2D;

    void Start ()
    {
       rigidbody2D = GetComponent<Rigidbody2D>();
    } 

    void Update () 

    {

        if (Input.GetKey(moveUp))

        {

            rigidbody2D.velocity.y = speed;

        }

        else if (Input.GetKey(moveDown))

        {

            rigidbody2D.velocity.y = speed * -1;

        }

        else

        {

            rigidbody2D.velocity.y = 0;

        }

    }

}

This doesn’t seem to fix my problem or errors. It is now giving me a Warning:

Assets/PlayerControls.cs(32,21): warning CS0108: PlayerControls.rigidbody2D' hides inherited member UnityEngine.Component.rigidbody2D’. Use the new keyword if hiding was intended

and my previous Errors are still there.

use this:

using UnityEngine;
using System.Collections;

public class PlayerControls : MonoBehaviour
{
	public KeyCode moveUp, moveDown;
	public float speed = 10.0f;
	
	void Update ()
	{
		Vector2 v = rigidbody2D.velocity;
		if (Input.GetKey(moveUp))
		{
			v.y = speed;
			rigidbody2D.velocity = v;
		}
		else if (Input.GetKey(moveDown))
		{
			v.y = speed * -1.0f;
			rigidbody2D.velocity = v;
		}
		else
		{
			v.y =  0.0f;
			rigidbody2D.velocity =v;
		}
	}
}

The error

Means you cant modify Rigidbody2D.velocity using something like Rigidbody2D.velocity=10

You need to put the objects Rigidbody2D.velocity in a temp variable and alter that variable

So set the temp variable like this:
Vector2 v = rigidbody2D.velocity;

Now v represents the velocity as a vector2 and you change this value by changing the x and y components of the vector as you want.
like this v.y = speed; then you just set the vector2 of rigidbody2d.velocity to this new value:

rigidbody2D.velocity = v;

Thank you so much softwizz, your code made it work! Also, thanks for your explanation on the error, you’ve really helped me out! I’ve learned a lot! :smile:.

You could shrink the code down to this if you wanted:

using UnityEngine;
using System.Collections;

public class PlayerControls : MonoBehaviour
{
	public float speed = 10.0f;
	
	void Update ()
	{
		Vector2 v = rigidbody2D.velocity; 
		if (Input.GetKey("w"))
		{
			v.y = speed; 
		}
		else if (Input.GetKey("s"))
		{
			v.y = -speed;
		}
		else
		{
			v.y = 0.0f;
		}
		rigidbody2D.velocity = v; 
	}
}

Glad to help.

I didnt explain it too well but if you understand what its doing then thats all thats matters.

Sorry for my mistake! I’ve used same keyword like system keyword. You can follow softwizz to resolve this problem :stuck_out_tongue:

Vector2 v = rigidbody2D.velocity;
if (Input.GetKey(moveUp))
{
v.y = speed;
rigidbody2D.velocity = v;
}
else if (Input.GetKey(moveDown))
{
v.y = speed * -1.0f;
rigidbody2D.velocity = v;
}
else
{
v.y = 0.0f;
rigidbody2D.velocity =v;
}
}
}[/code]

The error

Means you cant modify Rigidbody2D.velocity using something like Rigidbody2D.velocity=10

You need to put the objects Rigidbody2D.velocity in a temp variable and alter that variable

So set the temp variable like this:
Vector2 v = rigidbody2D.velocity;

Now v represents the velocity as a vector2 and you change this value by changing the x and y components of the vector as you want.
like this v.y = speed; then you just set the vector2 of rigidbody2d.velocity to this new value:

rigidbody2D.velocity = v;[/QUOTE]

Softwizz
Please help me.

I’m with the same error, see my script:

using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {

public float speed;
public float jumpspeed;
public Transform inicioraycast;
public Transform fimraycast;
public bool Chao;

// Use this for initialization
void Start () {
jumpspeed = 60;

}

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

Moviment ();

}
void Moviment ()
{
{ if (Input.GetAxisRaw (“Horizontal”) > 0)

transform.Translate (Vector2.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 0);
}
{ if (Input.GetAxisRaw (“Horizontal”) < 0)
transform.Translate (Vector2.right * speed * Time.deltaTime);
transform.eulerAngles = new Vector2 (0, 180);
}
if (Input.GetButtonDown (“Jump”))

{
rigidbody2D.velocity.y = jumpspeed;
}

}

void Raycasting()

{

Debug.DrawLine (inicioraycast.position, fimraycast.position, Color.red);
if (Physics2D.Linecast (inicioraycast.position, fimraycast.position))

{
Chao = true;
}
else
{
Chao = false;
}

}
}

Use code tags for posting code and tell me what line the error is on and what the error is.

help
Assets\player.cs(69,17): error CS1612: Cannot modify the return value of ‘Rigidbody2D.velocity’ because it is not a variable
these lines
public void Jump()
{
if(rb.velocity.y < 0.01f & rb.velocity.y > -0.01f)
{
rb.velocity.y += new Vector2(0, JumpSpeed);
GetComponent().Play();
}
}
please help

Please do not necro-post.

Go back up and read the answers already posted years and years ago.

Specifically this answer:

https://discussions.unity.com/t/525415/4

If that does not fix your post, DO NOT POST HERE, this thread is NINE YEARS OLD.

Start your own fresh post, and when you do, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)