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.
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
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:
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 () {
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