Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 27, 2018 at 06:19 AM by sean244 for the following reason:

Other

avatar image
0
Question by sean244 · May 27, 2018 at 02:52 AM · lightingeventdelegate

Destroy(gameObject) only works once

In my first script, I declare an event like so

 public static event Action OnChangeScene;

 private void OnMouseDown()
 {
      OnChangeScene?.Invoke();
 }

In my second script, I try to get Destroy(gameObject) to subscribe to that event via a lamda expression.

 private void Start()
 {
     MoveButton.OnChangeScene += () => Destroy(gameObject);
 }

The problem is that it only works the first time the mouse is clicked. After that, I get the error, "MissingReferenceException: The object of type 'Animal' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."

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

  • Sort: 
avatar image
0

Answer by jim3878 · May 27, 2018 at 03:03 AM

you need to unsubscribe event before destroy game object.

Try this

 private void Start()
  {
      MoveButton.OnChangeScene += DestortyGameObject;
  }
  
  private DestortyGameObject(){
     MoveButton.OnChangeScene -= DestortyGameObject;
     Destroy(gameObject);
  }


Comment
Add comment · Show 3 · 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 sean244 · May 27, 2018 at 03:23 AM 0
Share

But the whole point of using a lamda expression is so I wouldn't need to create a method.

avatar image jim3878 sean244 · May 27, 2018 at 03:06 PM 1
Share

In general, we recommend that you do not use anonymous functions to subscribe to events if you will have to unsubscribe from the event at some later point in your code.

https://docs.microsoft.com/en-us/dotnet/csharp/program$$anonymous$$g-guide/events/how-to-subscribe-to-and-unsubscribe-from-events

In my opinion ,to create a method seems rather feasible.

avatar image Bunny83 jim3878 · May 27, 2018 at 03:13 PM 0
Share

Further more anonymous methods actually do create a seperate method. Just that you can not directly access it. However the underlying generated code does have an actual method implementation with a compiler internal name. Also in this case the lambda expression is a closure. So you actually create a closure object as well

Follow this Question

Answers Answers and Comments

112 People are following this question.

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

Related Questions

Best approach to a interaction management system 1 Answer

How to unsubscribe from InputSystem event properly 1 Answer

Use Events or function or any best way ?? 1 Answer

Button onClick delegate with toggling method assignment 0 Answers

Cleanest way to decouple Manager class with events 1 Answer


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