- Home /
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
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 :/ Hope someone can help me. Thanks alot.NullReferenceException: Object reference not set to an instance of an object
Answer by Rod-Green · Dec 29, 2011 at 11:27 AM
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.
That, and if you're just wanting the bullet to self destruct a few seconds after instantiation, then why not just put the Destroy() call in awake/start ON the bullet with the script you're trying to access, rather than having this script tell the other one to destroy itself?
"DestroyBullet" is the script. I just did what the Unity reference says
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
startDestructionTimer Is never called, I added the Log for that case.
I cant use it on the awake function, because the object is being created and the user decides what to do with it (and so the time until it must be destroyed vary)
Never$$anonymous$$d I found a solution. I changed the script partly and just added the destruction sequence so I dont have to call it from another script. Better solution anyways. It was more a problem with the calling another script in general. I hope I dont have these problems next time.