Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ebil · Dec 29, 2011 at 11:21 AM · gameobjectdestroyshooting

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 :/ Hope someone can help me. Thanks alot.
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Dec 29, 2011 at 11:31 AM 0
Share

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?

avatar image Ebil · Dec 29, 2011 at 12:16 PM 0
Share

"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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

(in the end of the code...) when i try to run the program that tells me that the object is destoryed and i cant access it someone help? 1 Answer

How to delay lines of code. 2 Answers

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

Shooting Specific GameObject via Tag 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges