- Home /
Getting error: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable
I made a program using javascript, but I am curently learning C# so I wanted to transfer it over. I think I fixed most of the problems but this one: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable
using UnityEngine;
using System.Collections;
public class MYCLASSNAME : MonoBehaviour
{
int rotationSpeed= 100;
int jumpHeight= 8;
bool isFalling = false;
void Update ()
{
//Left to Right, A and D
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
rigidbody.AddRelativeTorque(Vector3.back * rotation);
if (Input.GetKeyDown (KeyCode.Space) && isFalling == false)
{
rigidbody.velocity.y = jumpHeight;
}
isFalling = true;
}
void OnCollisionStay ()
{
isFalling = false;
}
}
use the 101/010 button to format your code, so we don't have to try and figure it out.
Answer by rutter · Oct 20, 2013 at 10:03 PM
This:
rigidbody.velocity.y = jumpHeight;
Could be:
Vector3 v = rigidbody.velocity;
v.y = jumpHeight;
rigidbody.velocity = v;
You could name it whatever you want, but that's pretty much the temp variable pattern they're suggesting.
As an alternative, you can use AddForce() to accomplish roughly the same thing:
rigidbody.AddForce(Vector3.up * jumpHeight, ForceMode.VelocityChange);
this makes the object fly into the sky and never fall back down...?
oh, the variable I was using was a float, I don't know why but that made it totally flip out. Oh well. This was good, thanks.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
whats wrong with my code? 1 Answer
" ';' expected. Insert a semicolon at the end." Error Help 1 Answer
create variable trouble 1 Answer
(C#) Public variables that have descriptions in the engine. 2 Answers