سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

  1. First, it's "rigidbody" not "rigidBody"
  2. Second, starting with Unity 5 + something you cannot use "rigidbody" anymore, so you have to use GetComponent

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);
}

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);
}

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)*

  • I'm working in 2D right now, so my parameters of Vector3 might be off.
  • I thought Rigedbody was correct, but it might be rigidbody - see above.

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.

you need add rigidbody components in inspectors first then :

Rigidbody sphereRigidbody;
function Awake(){
sphereRigidbody = GetComponent<Rigidbody>();
sphereRigidbody.velocity = new  Vector3(15,0,0);
}

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);
    }

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; 

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top