Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Jasic560 · Mar 19, 2020 at 12:02 AM · nullreferenceexception

How do I check if an object is null?

I am making a game where enemies chase their target by moving towards them and rotating their gun towards them, but whenever the player is destroyed I get this error:

MissingReferenceException: The object of type 'Transform' 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.

This is the rotate gun script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class rotateEnemyGun : MonoBehaviour { public float offset; public Transform player;

 // Update is called once per frame
 void Update()
 {
     if (player != null)
     {
         Vector2 playerPos = player.transform.position;
         Vector2 lookPos = playerPos - (Vector2)transform.position;
         float rotZ = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;

         transform.rotation = Quaternion.AngleAxis(rotZ - offset, Vector3.forward);
     }
 }

}

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

6 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Mar 19, 2020 at 01:14 AM

I don't see any issues with the code you've posted. Are you sure the error is in this script? The code you've posted can't really be the cause of the error you've mentioned.

Comment
Add comment · Show 5 · 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 Jasic560 · Mar 19, 2020 at 01:29 AM 0
Share

There is no error in the script, the error is that when the player gets destroyed by the bullets from the enemy, it is trying to find the non existing player, but it can't find it, so it gives the error $$anonymous$$issingReferenceException: The object of type 'Transform' 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.

avatar image Bunny83 Jasic560 · Mar 19, 2020 at 12:02 PM 1
Share

You just contradicted yourself in your own paragtaph. Unity does never "try to find an object of type Transform" by it's own. It has be your code that does that and would cause a runtime exception if it fails. As I said the code you posted can't cause this issue because the error you see can only happen when an object that is referenced by a variable got destroyed. Your "not null" does catch destroyed objects properly so it can not enter the code where you try to access the transform.


When you get an error in the console during play you actually get a detailed stacktrace which tells you exactly where the error happened. It tells you the exact file and even the line number. You get that error, not us. You haven't included that information in your question so we can't tell you what your issue is. All I can tell you is that the code you've posted can not cause this error.


So again are you sure the error comes from the script you've posted? Can you include the stacktrace from the error you got? Note that you can select and copy text from the bottom half of the console window.

avatar image Jasic560 Bunny83 · Mar 19, 2020 at 03:11 PM 0
Share

Oh ok, I'll look in to that. I'll send you the full error: $$anonymous$$issingReferenceException: The object of type 'Transform' 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. UnityEngine.Transform.get_position () <0x24abe250e90 + 0x0006a> in :0 EnemyAI.Update () (at Assets/Scripts/Enemy/EnemyAI.cs:26)

Show more comments
avatar image
1

Answer by niklasdette · Mar 19, 2020 at 12:22 AM

It could be the following reason, but not 100% sure:

You are checking if the player is "null". But you are deleting the player. So the player is not "null" it just has a "missing reference" (you should be able to see it in the inspector). So you should maybe set the player manually to "null" before destroying it.

I hope it works :)

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 Jasic560 · Mar 19, 2020 at 12:24 AM 0
Share

How would I do that?

avatar image niklasdette Jasic560 · Mar 19, 2020 at 12:29 AM 0
Share

Create a refence to the script where you are destroying it.

In this case you would create a variable in the script named:

public rotateEnemyGun variableName; //This you would assign of course for example in the inspector per trag and drop

and then before you are destrying the player: variableName.player = null;

avatar image Jasic560 niklasdette · Mar 19, 2020 at 12:51 AM 0
Share

Doesn't look like it worked, thanks for trying.

avatar image
1

Answer by The-Burgunfaust · Mar 19, 2020 at 03:52 PM

Don't destroy the player, just disable it.

Comment
Add comment · Show 4 · 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 Jasic560 · Mar 21, 2020 at 12:44 AM 0
Share

I might try that

avatar image FuryFight3r · Mar 21, 2020 at 07:31 AM 0
Share

I would have to agree with The-Burgundaust, Unless your game finishes and you cant play again after you die until you restart the game (restart meaning close application and reopen), then destroying the character is fine, but if you want lives, or if you want to move the character back to the start, then defintly just disable it, destroying it means exactly that.. its destroyed and you can no longer use it. Destroying gameobjects is for objects that you never want to use again during that run-time.. so if you do want to use the charcter after it has died do not destroy it, just turn it off for the time being.

avatar image da_egg5 · Dec 10, 2021 at 11:43 AM 0
Share

I know this comment was a year ago, but how exactly do I disable it. I'm running into the exact same issue, but I can't quite find a Disable command.

Thank you!

avatar image FuryFight3r da_egg5 · Dec 10, 2021 at 10:29 PM 2
Share

The way to disable game objects is:

gameObject.SetActive(false);

And the below will turn the object back on

gameObject.SetActive(true);

avatar image
-5

Answer by Cyber-Spyder · Mar 19, 2020 at 12:12 AM

@Jasic560 Try If (player = null) { return }

that might fix it

Comment
Add comment · Show 1 · 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 Jasic560 · Mar 19, 2020 at 12:22 AM 0
Share

I've already tried that, it didn't work.

avatar image
0

Answer by Xpartano · Mar 19, 2020 at 12:57 AM

Have you tried if (player) instead of if (player != null) ?

Comment
Add comment · Show 1 · 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 Jasic560 · Mar 19, 2020 at 01:38 AM 0
Share

Yeah, but it didn't work either.

  • 1
  • 2
  • ›

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

132 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 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

itween example work well on unity3d while NullReferenceException on android 0 Answers

GameObject.Find/WithTag returns null 1 Answer

Adding sound to animation: NullReferenceException 0 Answers

Why am I getting a NullReferenceException ? 1 Answer

GUI.Label not set to an instance 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