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 Supershandy · Apr 12, 2013 at 12:45 PM · javascriptnullreferenceexceptionnullfindgameobjectwithtag

how to fix a null reference exception when a findgameobjectwithtag does not exist

Hi there, i'm having a slight problem with part of my code for a game i'm developing.

The basic premise is that an object will intercept another object is within range, quick note is that i'm not using sphere colliders for this purpose as they interfere with lens flare effects.

I have a public variable set up as "Target : Gameobject" andd then

Target = findgameobjectwithtag ("Friendly")

If (Target.distance < RadarRange) { Intercept() }

If (!Target) { Patrol() }

However, when the object (Friendly) has been destroyed i get a null reference exception and the game slows down to a crawl with then enemy objects just flying off in a straight line and not patrolling. Is there anyway to fix this so that when the target variable is null that the enemy fighters go back to patrolling?

Forgive me for the spelling i'm on my mobile and at work, plus i've tried to remember the code as best i can since i don't have it to hand but i'm sur you can get the basic gist of wht i'm trying to do.

Many 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 Supershandy · Apr 12, 2013 at 12:46 PM 0
Share

I've also tried if (Target == null) as well

1 Reply

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

Answer by Dave-Carlile · Apr 12, 2013 at 01:06 PM

Seems like you're on the right track...

 Target = FindGameObjectWithTag("Friendly");
 if (Target == null)
   Patrol();
 else if (Target.distance < RadarRange)
   Intercept();
 else
   Patrol();

In English, if there is no target then patrol, otherwise if there is a target within the radar distance then intercept, otherwise there is a target, but it's too far, patrol. This code can be simplified a bit...

 Target = FindGameObjectWithTag("Friendly");
 if (Target != null && Target.distance < RadarRange)
   Intercept();
 else
   Patrol();

Same idea, but it does the target existence and distance check in a single line with "and".

Comment
Add comment · Show 7 · 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 Supershandy · Apr 12, 2013 at 01:24 PM 0
Share

I'll try it when i get home, but it looks and sounds correct

avatar image Supershandy · Apr 12, 2013 at 06:15 PM 0
Share

Just tried it, it still didn't work, here is the section of code with the refinements, the problem is in the Target variable reference : Target = GameObject.FindWithTag ("Friendly").transform;

 function FixedUpdate () 
 
 {
     rigidbody.AddForce (transform.forward * (PatrolSpeed * Time.deltaTime));
     
     Target = GameObject.FindWithTag ("Friendly").transform;
     
     if (Target != null && Vector3.Distance (Target.position, transform.position) < RadarRange)
     {
         IsIntercepting = true;
         IsPatrolling = false;
         rigidbody.AddForce (transform.forward * (InterceptSpeed * Time.deltaTime));
         Intercept();
         //Debug.Log("Radar Contact, $$anonymous$$oving to Intercept - Red Fighter"
     }
     else
     {
         IsAttacking = false;
         IsIntercepting = false;
         Patrol();
     }
avatar image Dave-Carlile · Apr 12, 2013 at 06:53 PM 0
Share

Which line causes the null reference exception?

avatar image Supershandy · Apr 12, 2013 at 06:54 PM 0
Share

line 6 is what is causing the problem

avatar image Dave-Carlile · Apr 12, 2013 at 07:48 PM 1
Share

It's the .transform part of your code. The function call to FindWithTag is returning null, then you're trying to access the transform property. You need to split that into two pieces. Get the game object itself. If that's null then patrol. If it's not null then you can safely get the transform...

 GameObject go = GameObject.FindWithTag("Friendly");

 if (go == null)
   Patrol();  
 else
 {
   Transform target = go.transform;
   .... the rest of your intercept code...
 }
Show more comments

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

12 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

Related Questions

NullReferenceException MaskableGraphic.UpdateCull() upon Loading Player Data. 0 Answers

Why can't I access the TextMesh component of a GameObject? 1 Answer

Enemy not giving damage 2 Answers

Why is this null? Finding a script on an object 3 Answers

Accessing enum from editor script returns null 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