- Home /
Question by
Turbostrzel · Jul 05, 2016 at 06:31 PM ·
javascriptrigidbody2dambiguous reference
Java Script: Ambiguous reference
Hi
I'm a beginner and I'm trying to write a simple code for jumping using AddForce, but I don't understand what does this error mean:
Assets/Jumping.js(16,17): BCE0004: Ambiguous reference 'rigidbody': Jumping.rigidbody, UnityEngine.Component.rigidbody.
public var jumpPower : float;
private var rigidbody : Rigidbody2D = GetComponent(Rigidbody2D);
function Start()
{
jumpPower = 50;
}
function Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
rigidbody.AddForce(Vector3.up * 300);
}
}
Comment
Best Answer
Answer by Eric5h5 · Jul 05, 2016 at 07:38 PM
Unity used to have shortcut component accessors, one of which was called "rigidbody", which was short for GetComponent(Rigidbody). Rename the "rigidbody" variable to something else. Also, it's best not to attempt to run code outside functions; only declare variables. It should be:
private var myRigidbody : Rigidbody2D;
function Start() {
myRigidbody = GetComponent(Rigidbody2D);
....
Your answer
![](https://koobas.hobune.stream/wayback/20220612084257im_/https://answers.unity.com/themes/thub/images/avi.jpg)