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 /
  • Help Room /
This question was closed Jan 29, 2017 at 01:41 PM by Rekrahttam1 for the following reason:

I solved the issue.

avatar image
0
Question by Rekrahttam1 · Jan 29, 2017 at 01:01 PM · c#crashnullreferenceexceptionnull reference exceptioninvoke

(C#) Invoke giving null reference exception, crashing unity

I have looked through many answers on this and other forums, but none seem to match my situation.

My original code was a fair bit more complex, so I commented out most of it (which still causes the error) to just:

 public void Test() {
     print("Test complete");
 }

 public void fire() {
     print("Just before Invoke");
     Invoke("Test", 1);
 }

which is called from another script:

 void Update() {
     //if (Input.GetMouseButtonDown(0) && weaponsCarried[currentWeapon].readyToFire) {
     if (Input.GetMouseButtonDown(0)) {
         print("fire");
         weaponsCarried[currentWeapon].fire();
     }
 }

The Invoke causes a null reference exception, which then causes unity to not respond and crash when I end the game (but I can still continue playing the game despite the error). The exact error is (line 55 is the Invoke):

 NullReferenceException
 Weapon.fire () (at Assets/Scripts/Weapons.cs:55)

The code works perfectly without the Invoke statement. All print statements except that in Test() do show up.

Is this not the exact way that Invoke is supposed to be used? I am lost as to why this is not working!?

Any help would be greatly appreciated! Thanks!

Comment
Add comment · Show 1
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 Rekrahttam1 · Jan 29, 2017 at 01:41 PM 1
Share

I solved the issue. Since the Invoke call was made within a class that had been instantiated, references to $$anonymous$$onoBehaviour do not work. It appears that $$anonymous$$onoBehaviour cannot be copied into the new class created, and so exists only in a class created by attaching to a gameObject as a component.

$$anonymous$$y fix is to call a function that is within a script attached to a gameObject, which starts another function in a coroutine and yields until the proper time, which then calls the required function back in the instantiated class. I had to use StartCoroutine and yield to allow me to pass the references to what had to be invoked after the time elapsed, as Invoke does not take parameters, and so the callback cannot be dynamically modified.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Null Reference Exception on Sprite Renderer 0 Answers

Faulty NullReferenceException? 2 Answers

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers

Having a issue with a NullReferenceException 1 Answer

Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 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