- Home /
rigidbody' is not a member of 'UnityEngine.Object ?
I'm geting the error:
Assets/_Scripts/LookAtTarget.js(38,16): BCE0019: 'rigidbody' is not a member of 'UnityEngine.Object'.
and here's the code and that line 38 is near the bottom:
var LookAtTarget:Transform; var damp = 6.0; var bullitPrefab:Transform; var savedTime=0;
function Update () { if(LookAtTarget) { var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}
function Shoot(seconds) { if(seconds!=savedTime) { var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position , Quaternion.identity); bullit.rigidbody.AddForce(transform.forward * 1000); savedTime=seconds; }
}
I'm not sure what I've done wrong here, spent a few hours on this one...
Thank you.
Answer by Murcho · Mar 12, 2010 at 11:28 PM
Instantiate() returns type Object, not type GameObject as your code assumes, and as you are not using strong typing on your variable declaration of bullit, it is being created as an Object type.
I haven't coded in UnityScript for a while, so I can't quite remember the correct casting method, but I'm pretty sure this is right.
var bullit : GameObject = Instantiate(bullitprefab, transform.Find("spawnPoint").position, Quaternion.identity);
This code should cast the Object returned by Instantiate() to a GameObject, which will allow your follow code to work properly.
Hey thanks a lot. I'm getting this now though:
Assets/_Scripts/LookAtTarget.js(36,47): UCE0001: ';' expected. Insert a semicolon at the end.
But there is a semicolon there...
Yes I'm obviously new to Unity coding hehe.
Thanks Eric, I couldn't remember the correct casting for JS. Obviously JS sorts it out properly as long as you declare the variable type properly.
Answer by Flimgoblin · Mar 13, 2010 at 07:56 PM
You need to either do:
var bullit:GameObject= Instantiate(...);
or
var bullit = Instantiate(...) as GameObject;
was running into this issue when I was trying to Instantiate objects on the Android.. During PC runtime no errors would fireoff. Just added the casting and whoo hoo. All good!! 'xxxxxxxx' is not a member of 'UnityEngine.Object'. all fixed!!
Answer by shane · Mar 13, 2010 at 08:36 PM
Ok.. haha where does that go in these two lines?:
var bullit : GameObject = (GameObject)Instantiate(bullitprefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
I tried using your two examples replacing var bullut : GameObject = but didn't know how to structure it properly and got more errors.
cheers.
You should be using comments rather than posting new answers. I edited $$anonymous$$urcho's answer so it should work.
Answer by Min Won Ki · Apr 12, 2010 at 09:46 AM
You change source
var bullitPrefab:Transform;
var bullitPrefab:GameObject;
var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position , Quaternion.identity);
var bullit:GameObject = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position , Quaternion.identity) as GameObject;
and
re-seting bullitPrefab object in Main Camara