- Home /
Unity Android Error on rigidbody.AddForce?
Hello, I have a problem with Unity Android. One time when I was building the game it aborted the building and said that there were errors. This is the error -
Assets/Dodger/Scripts/iTemSpawn.js(25,22): BCE0019: 'rigidbody' is not a member of 'UnityEngine.Object'.
And part of my script says-
var item = Instantiate(itemPrefab, Vector3(x,y,transform.position.z), Quaternion.identity);
item.rigidbody.AddForce(-Vector3.forward * force);
Please help me!
It's quite a story how it got there and I never knew it was possible.
Answer by Rydrako · May 23, 2011 at 02:13 AM
Wow, I figured it out! So I cant use this-
var item = Instantiate(itemPrefab, Vector3(x,y,transform.position.z), Quaternion.identity);
item.rigidbody.AddForce(-Vector3.forward * force);
but I can change the-
var item = Instantiate(itemPrefab, Vector3(x,y,transform.position.z), Quaternion.identity);
into this-
var item : Transform = Instantiate(itemPrefab, Vector3(x,y,transform.position.z), Quaternion.identity);
And I was just messing around with the code!
Yes. You just need to specify the type of object to Instantiate()
to. I believe you could also use GameObject
ins$$anonymous$$d of Transform
. This is similar to the use of GetComponent()
, where you need to specify the type of component when changing a specific component's variables. See http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html for more on this unrelated topic. :)