- Home /
Question by
DolphinDudeOne · Jul 12, 2014 at 03:34 AM ·
javascriptrigidbodyaddforcevar
Declaring myRigidbody
I so far have this script to make my monster move but it says that there is an unknown identifier "myRigidbody".
#pragma strict
var target : Transform;
var moveSpeed = 240;
var rotationSpeed = 180;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myRigidbody.AddForce(myTransform.forward * moveSpeed * Time.deltaTime);
}
Could somebody show me how to identify it or where I went wrong scripting.
Comment
Best Answer
Answer by robertbu · Jul 12, 2014 at 03:38 AM
If you double click on the error in the console, it will take you to the error. In this case it is on line 24. You have not declared 'myRigidbody'. You can use 'rigidbody' instead. Rigidbody is a shortcut that under the hood it is equivalent to:
GetComponent(Rigidbody);
Note you can avoid the GetComponent() call every time you use it by doing the same thing that you did with myTransform. That is declare at the top of the file at line 7:
var myRigidbody : Rigidbody;
And then in Start() do:
myRigidbody = rigidbody;
Then line 24 will work unchanged.