Destruct gameobject by call from another script

Hello everyone,

I got problems by destroying an instantiated game object from another script.

I got my shooting script, which also should call the destruction with these lines of code:

	if(Input.GetKey("g"))

{

	var destroyBullet : DestroyBullet = GetComponent("DestroyBullet");

	Debug.Log("Request for DestroyTimer");

	destroyBullet.startDestructionTimer();

}

It is just a testing area, normally the destruction should start automatically after a value of another script is proven.
But ok the destruction sequence of DestroyBullet:

Debug.Log("=========================================================");
function startDestructionTimer() {
Debug.Log("DestroyTimerStarted");
	Destroy(gameObject, 3);
}

I also added Logs, because I wanted to see where it stops.

It looks like the function cannot be called somehow, the destruction alone (for example in awake function) works perfectly, also the key pressing (“g”) works.
But it just cannot call the script
I get the following error

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object args, System.String cacheKeyName, 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)
magicShooting.Update () (at Assets/WeaponScripts/magicShooting.js:31)

Line 31 is this one
destroyBullet.startDestructionTimer();

I also declared the startDestructionTimer function public but it did not help :confused:

Hope someone can help me. Thanks alot.

Is “destroyBullet” null by any chance? Are you sure there’s a “DestroyBullet” component on this object?

Maybe the “startDestructionTimer” is being called twice and on the second time failing.

There could be lots of things wrong. You might need to post a more complete sample that we can test locally.