- Home /
Error when trying to instantiate
Heres the code:
var BulletPrefab : Transform;
function Update () { if(Input.GetButtonDown("Fire1")) { var Bullet = Instantiate( BulletPrefab, GameObject.Find("Barrel").transform.position, Quaternion.identity); Bullet.rigidbody.AddForce(transform.forward * 2000); } }
and heres the error:
MissingMethodException: Method not found: 'UnityEngine.Rigidbody.AddForce'. Boo.Lang.Runtime.MethodDispatcherFactory.ProduceExtensionDispatcher () Boo.Lang.Runtime.MethodDispatcherFactory.Create () Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args) [0x00000] Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args) [0x00000] Boo.Lang.Runtime.RuntimeServices+<>c_DisplayClass1.b_0 () Boo.Lang.Runtime.DispatcherCache.Get (Boo.Lang.Runtime.DispatcherKey key, Boo.Lang.Runtime.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000] Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000] Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) [0x00000] UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) [0x00000] GunControl.Update () (at Assets/Game/Scripts/GunControl.js:8)
I have no freaking idea what to do!
P.S. how do I make the Code thing work in questions?
you just select your code, and click the code button when editing (has lots of 0 and 1s) - i'll hang off editing the post so you can do it
Na man, can't do it, I can only make 1 line work... :(
Answer by Mike 3 · Jun 24, 2010 at 08:24 AM
You need to force the type of Bullet - Instantiate returns UnityEngine.Object, which could be (nearly) anything, and javascript is getting confused
var Bullet : Transform = Instantiate( BulletPrefab, GameObject.Find("Barrel").transform.position, Quaternion.identity);
Alternatively you could declare your BulletPrefab as type GameObject.