Reputation: 57
I'm working with an android project in Unity 3d. I would to roll the sphere at the surface of a cube. However, when I clicked the play button it returns error message:
Assets/Scripts/Player.js(4,1): BCE0005: Unknown identifier: 'rigidBody'.
My code:
function Start () {
rigidBody.velocity.x=15;
}
Rigidbody components has been already added to the sphere. I would like to seek solution to the error generated.
Upvotes: 1
Views: 1093
Reputation: 7442
if you working with unity less than 5 (I guess) you have access to use components of game objects like rigidbody or audiosource but in unity 5 and later you need to add a reference to that in neither in awake or start function like this code
private Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
// AND AFTER YOU ADDED THE REFERENCE FOR RIGIDBODY
// THEN CHANGE THE VELOCITY LIKE THIS
rb.velocity.x = 20;
}
Upvotes: 0
Reputation: 468
Resuming, to use "rigidBody" as it is, you have to initialize it first like others answered you already:
//link you rigidbody here:
public Rigidbody rigidBody;
function Start() {
//Or if the script is on the GameObject that has the rigidbody component:
//rigidBody = GetComponent<Rigidbody>();
rigidBody.velocity=new Vector2(15,0);
}
Upvotes: 2
Reputation: 15
for c#
You might want to cache it first.
private Rigidbody rigidbodyCached;
//cache
void Start(){
rigidbodyCached = this.GetComponent<Rigidbody>();
}
//for velocity movements use FixedUpdate instead of Update
void FixedUpdate(){
rigidbodyCached.velocity = new Vector3(15,0,0);
}
Upvotes: 0
Reputation: 1
you need add rigidbody components in inspectors first then :
Rigidbody sphereRigidbody;
function Awake(){
sphereRigidbody = GetComponent<Rigidbody>();
sphereRigidbody.velocity = new Vector3(15,0,0);
}
Upvotes: 0
Reputation: 207
I think you forgot to initialize a Rigidbody. Also you cannot assign a velocity like this because rigidBody.velocity.x is a read-only value.This code might help you:
public Rigidbody rigidBody;
function Start(){
rigidBody.velocity=new Vector2(15,0);
}
Upvotes: 1
Reputation: 4821
You haven't initialized the variable "rigidBody". I don't think that's your objective though. If you have the script added to the sphere as a component, you don't have to use getComponent. Instead it's going to be just:
"Rigidbody.velocity.x=15;"
You might have to use a "new Vector3(x,y,z);" to pass the new velocity on. In that case the code would look like this:
Rigidbody.velocity = new Vector3(15,Rigidbody.velocity.y, Rigidbody.velocity.z)*
In any case, don't forget your colliders. Ridged bodies don't automatically collide with other objects, but they are subject to gravity. Once I finally figured that out, I just dropped my character 20 feet onto pavement out of joy. Rendering blood is surprisingly easy if you're not that picky.
Upvotes: 0
Reputation: 173
I don't know if you've set a GetComponent variable on the rigidbody, but you may have to strip the case out of that.
For example:
rigidBody.velocity.x=15;
would be:
rigidbody.velocity.x=15;
Hope that helps.
Upvotes: 2