- Home /
Build and run question, iOS, newbie question
I've made a little game - a very typical spaceship shooter (like space invaders - to try something completely new :))
Anyway - the game works just fine when I test it in unity and I get no compiling errors in the console, but when I chose to build and run to test the game on the iPhone i get these two messagea in the console:
Assests/flyBulletFly.js(10,32): BCE0019: 'rigidbody' is not a member of 'UnityEngine.Object".
Error building Player because scripts had compiler errors UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()
the script the message refers to looks like this:
var bulletSpeed : float; var prefabBullet : Transform;
function Update () {
if(Input.GetButtonUp("Jump")){
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
Can someone help me please!
Thanks Jeppe (upcoming developer taking baby steps)
Answer by Eric5h5 · Dec 05, 2010 at 04:31 PM
Put "#pragma strict" at the top of all scripts when using JS with an iOS (or Android) target, and fix all resulting dynamic typing errors. For example, Instantiate returns Object, which is making instanceBullet be type Object, which works OK with dynamic typing (except slower), but won't work without dynamic typing. So you need to cast that to a GameObject in this case.
I finally got my head around static and dynamic variables - thank you for pointing me in the right direction
Answer by JeppeNygaard · Dec 05, 2010 at 05:40 PM
Put "#pragma strict" at the top of all scripts when using JS with an iOS (or Android) target
Thank you for this! ... when I add the "#pragma strict" the compiler will no longer accept my script, again referring to: 'rigidbody' is not a member of 'UnityEngine.Object".
What am I overlooking - rigidbody should indeed be part of the unityEngine?
the script looks like this:
var bulletSpeed : float; var prefabBullet : Transform;
function Update () {
if(Input.GetButtonUp("Jump")){
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
Can you help me?
Thanks Jeppe
As I said, you need to cast to a GameObject. rigidbody is not a member of UnityEngine.Object, just like the error says. It's a member of UnityEngine.GameObject. So declare instanceBullet to be a GameObject. In the future, comment on my answer or edit your question ins$$anonymous$$d, since this isn't an answer.
Hi Eric,
Thank you so much for your answer. I've tried declaring instanceBullet as a GameObject ins$$anonymous$$d only to find even more errors. I know this may be way too much to ask but would you $$anonymous$$d giving me another hint? maybe even a concrete suggestion to how I could re-write my script? ... it would be a tremendous help!
Answer by thung · Feb 18, 2011 at 05:36 PM
You have to type the Transform as a GameObject AND type the Instantiate function as a GameObject as well. I didnt need to add "#pragma strict" to make it work
var bullet:GameObject; var shootForce:float = 10;
function Update() {
if(Input.GetMouseButtonDown(0))
{
var instanceBullet = Instantiate(bullet,transform.position,Quaternion.identity) as GameObject;
instanceBullet.rigidbody.AddForce(transform.forward *shootForce);
}
}