- Home /
Missing Method Exception and Null Reference Exception
I was going through the tornadotwins tutorials on youtube, and for some reason their scripts keep giving me errors? I solved most of them but 2, a Missing Method Exception and Null Reference Exception.
The Missing Method Exception applied to my Turret control script, heres the script
var LookAtTarget : Transform;
var damp : float = 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);
transform.LookAt(LookAtTarget);
}
}
function Shoot(seconds)
{
if(seconds!=savedTime)
{
var bullit = Instantiate(bullitPrefab,transform.Find("TurretSpawnPoint").transform.position , Quaternion.identity);
bullit.rigidbody.Addforce(transform.forward * 1000);
}
savedTime=seconds;
}
And the error:
MissingMethodException: Method not found: 'UnityEngine.Rigidbody.Addforce'. Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher () Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create () Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices+c_AnonStorey12.m_6 > () > Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get > (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey > key, > Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory > factory) > Boo.Lang.Runtime.RuntimeServices.Dispatch > (System.Object target, System.String > cacheKeyName, System.Type[] > cacheKeyTypes, System.Object[] args, > Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory > factory) > Boo.Lang.Runtime.RuntimeServices.Dispatch > (System.Object target, System.String > cacheKeyName, System.Object[] args, > Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory > factory) > Boo.Lang.Runtime.RuntimeServices.Invoke > (System.Object target, System.String > name, System.Object[] args) > UnityScript.Lang.UnityRuntimeServices.Invoke > (System.Object target, System.String > name, System.Object[] args, > System.Type scriptBaseType) > TurretControl.Shoot (System.Object > seconds) (at > Assets/Scripts/TurretControl.js:26) > TurretControl.Update () (at > Assets/Scripts/TurretControl.js:15)Assets/Scripts/TurretControl.js:15)
The next is a NullReferenceException for my MoveAround Script, heres the script, It took a while to get a solution for this. I dont know why the scripts arent working for me, they seem to work fine for most people...
var speed = 3.0;
var rotateSpeed = 3.0;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
if(Input.GetButtonDown("Fire1"))
{
Instantiate (GameObject.Find("BubblePrefab"),
GameObject.Find("SpawnPoint").transform.position,
Quaternion.identity);
}
}
@script RequireComponent(CharacterController)
And le error... NullReferenceException UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:46) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/BaseClass.cs:57) MoveAround.Update () (at Assets/Scripts/MoveAround.js:15)
Answer by Borgo · Jun 23, 2011 at 09:23 PM
MissingMethodException: Method not found: 'UnityEngine.Rigidbody.Addforce' The correct is AddForce, with "F" and not "f".
... facepalm Well now i feel like an idiot, it took ages for me to attempt to figure it out, and thats all it was? Idiot me....
Answer by save · Jun 23, 2011 at 09:37 PM
The missing method is because Unity is case-sensitive, you call:
bullit.rigidbody.Addforce
when it should say:
bullit.rigidbody.AddForce
The second error is most likely that you've not wrapped up your prefab into a variable. Have a look at Instantiating Prefabs at Runtime in the docs.
On lines 15-17, instead of doing this:
Instantiate (GameObject.Find("BubblePrefab"),
GameObject.Find("SpawnPoint").transform.position,
Quaternion.identity);
you should do this:
Instantiate (bubblePrefab, spawnPoint.transform.position, Quaternion.identity);
Where you first declare bubblePrefab and spawnPoint as variables outside the Update-function:
var bubblePrefab : GameObject;
var spawnPoint : Transform;
Now it says The referenced script on this Behaviour is missing! UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) $$anonymous$$oveAround:Update() (at Assets/Scripts/$$anonymous$$oveAround.js:17)
And it wont let me assign the spawn point i created to the script. Its an empty gameobject just in front of the character
$$anonymous$$ake sure that the var spawnPoint is set as : Transform in the script (as provided in the example).
Answer by XXXar300500XXX · Jun 01, 2013 at 10:30 AM
on the first on i did have AddForce
but u keep getting this error NullReferenceException UnityEngine.Component.get_transform () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:21) ControlTurret.Shoot (System.Object seconds) (at Assets/Turret/ControlTurret.js:30) ControlTurret.Update () (at Assets/Turret/ControlTurret.js:19)
my javascript:
var LookAtTarget:Transform; var damp = 1; var bullitPrefab:Transform; var saveTime=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 != saveTime) { var bullit = Instantiate(bullitPrefab, transform.Find("spawnpoint").transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 1000);
saveTime=seconds;
}
}
Your answer
Follow this Question
Related Questions
Null Reference Exception Hunt ! 1 Answer
TornadoTwins Tutorial 1 Answer
how to create Objectives 1 Answer
Enemies Problem 1 Answer
GUI_TEXTURE PROBLEM! 2 Answers