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 · May 06, 2013 at 02:22 PM · javascriptainullreferenceexceptioncomponent

Very strange NullReference Problem

Sorry to keep asking questions but this one has me truly stumped.

I managed to get a pursuit AI function working for one of my AI's, but when I copied the portion of the script over to the other AI objects and changed the GetComponent section to look at the other AI's script to access it, I keep getting a weird NullReferenceException saying that nothing has been referenced even though all the spelling is correct and the script is attached to the AI.

Here are the code sections

EnemyAI - Working

 function Intercept()
 
 {
     var Target : Transform = target.transform;
     var rotation = transform.rotation;
     var position = transform.InverseTransformPoint(Target.position);
     var direction : float = position.x > 0 ? 0.01 : - 0.01;
     var angle = Vector3.Angle (Vector3.forward, position)* direction;
     var targetDirection = (Target.position - transform.position).normalized;
     var lookRotation = Quaternion.LookRotation (targetDirection);
     transform.rotation = Quaternion.Slerp(rotation, lookRotation, TurnSpeed * Time.deltaTime);
     transform.Rotate (0,0, -angle * Roll);
     
     
     if (Vector3.Distance(Target.position, transform.position) <= VisualRange)
     {
         transform.Translate (0, 0, AttackSpeed * Time.deltaTime);
         IsIntercepting = false;
         //Debug.Log("Visual Contact! Moving to Attack - Red Fighter");
         IsAttacking = true;
         Attack();
     }
     if (Vector3.Distance(Target.position, transform.position) <= RadarRange || Vector3.Angle(Target.position, transform.position) < 10)
     {
         var EnemySpeed = Target.GetComponent(FriendlyAI).InterceptSpeed;
         var TargetFuturePosition = Target.position + targetDirection * EnemySpeed;
         var FutureDirection : Vector3 = TargetFuturePosition - transform.position;
         transform.Translate (0, 0, InterceptSpeed * Time.deltaTime);
         var lookFutureRotation = Quaternion.LookRotation (FutureDirection);
         transform.rotation = Quaternion.Slerp(rotation, lookFutureRotation, TurnSpeed * Time.deltaTime);
         transform.Rotate (0,0, -angle * Roll);
         IsIntercepting = true;
         IsPatrolling = false;
         IsEvading = false;
         IsAttacking = false;
     }
     
     if (Vector3.Distance (Target.position, transform.position) > RadarRange)
     {
         IsIntercepting = false;
         //Debug.Log("Radar contact lost, returning to patrol - Red Fighter");
         IsPatrolling = true;
         transform.Translate (0, 0, PatrolSpeed * Time.deltaTime);
         Patrol();
     }
 }

Friendly AI - Not working

 function Intercept()
 
 {
     var Target : Transform = target.transform;
     var rotation = transform.rotation;
     var Position : Vector3 = transform.InverseTransformPoint(Target.position);
     var Direction : float = Position.x > 0 ? 0.01 : -0.01;
     var Angle = Vector3.Angle (Vector3.forward, Position) * Direction;
     var targetDirection = (Target.position - transform.position).normalized;
     var lookRotation = Quaternion.LookRotation (targetDirection);
     transform.rotation = Quaternion.Slerp(rotation, lookRotation, TurnSpeed * Time.deltaTime);
     transform.Rotate (0, 0, -Angle * Roll);
     
     if (Vector3.Distance(Target.position, transform.position) <= VisualRange)
     {
         transform.Translate (0, 0, AttackSpeed * Time.deltaTime);
         IsIntercepting = false;
         //Debug.Log("Visual Contact! Moving to Attack - Blue Fighter");
         IsAttacking = true;
         Attack();
     }
     
     if (Vector3.Distance(Target.position, transform.position) <= RadarRange || Vector3.Angle(Target.position, transform.position) < 10)
     {
         var EnemySpeed = Target.GetComponent(EnemyAI).InterceptSpeed;
         var TargetFuturePosition = Target.position + targetDirection * EnemySpeed;
         var FutureDirection : Vector3 = TargetFuturePosition - transform.position;
         transform.Translate (0, 0, InterceptSpeed * Time.deltaTime);
         var lookFutureRotation = Quaternion.LookRotation (FutureDirection);
         transform.rotation = Quaternion.Slerp(rotation, lookFutureRotation, TurnSpeed * Time.deltaTime);
         transform.Rotate (0,0, -Angle * Roll);
         IsIntercepting = true;
         IsPatrolling = false;
         IsEvading = false;
         IsAttacking = false;
     }
     
     if (Vector3.Distance (Target.position, transform.position) > RadarRange)
     {
         IsIntercepting = false;
         //Debug.Log("Radar contact lost, Returning to patrol - Blue Fighter");
         IsPatrolling = true;
         transform.Translate (0, 0, PatrolSpeed * Time.deltaTime);
         Patrol();
     }
 }

Now this is where the problem is

Enemy AI - Working

 var EnemySpeed = Target.GetComponent(FriendlyAI).InterceptSpeed;

Friendly AI - Not Working

 var EnemySpeed = Target.GetComponent(EnemyAI).InterceptSpeed;

Both InterceptSpeed are public variables, yet why does it work for one and not for the other?

Comment
Add comment · Show 16
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 cagezero · May 06, 2013 at 02:32 PM 0
Share

$$anonymous$$y guess is that this line is failing somehow.

 var Target : Transform = target.transform;
avatar image Supershandy · May 06, 2013 at 02:35 PM 0
Share

But that's just it, The Enemy and Friendly AI scripts are just carbon copies of each other, except for small, $$anonymous$$or changes like FindWithTag "Enemy" and FindWithTag "Friendly", there are no major differences.

And with the EnemyAI working without problems yet the FriendlyAI isn't is confounding me a little bit, both have been working fine up until this point

avatar image cagezero · May 06, 2013 at 02:38 PM 0
Share

Have you verified that lowercase target is indeed set? If not that should give you a good starting point for tracking the problem.

avatar image Supershandy · May 06, 2013 at 02:48 PM 0
Share

Just went back through the variables at the top of the script, both target variables are set correctly

Enemy AI private var target : GameObject;

Friendly AI private var target : GameObject;

avatar image cagezero · May 06, 2013 at 08:04 PM 0
Share

Will see what we can do...

Please replace this line of code:

 var EnemySpeed = Target.GetComponent(FriendlyAI).InterceptSpeed;

with the following statements:

 Debug.Log ("Tag name: " + Target.gameObject.tag);
 var aiScript = Target.GetComponent(FriendlyAI);
 if(aiScript == null)
     Debug.Log ("AI Script is null");
 else
     Debug.Log ("Friendly Speed: " + aiScript.InterceptSpeed);

and report the results.

Show more comments

2 Replies

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

Answer by cagezero · May 08, 2013 at 07:51 PM

You are using

 target = GameObject.FindWithTag ("Enemy");

To acquire the EnemyFighter. However, EnemyFighter has a child object also tagged "Enemy". According to the docs FindWithTag "Returns one active GameObject tagged tag." It seems you are getting lucky with one script and unlucky with the other! I was able to get things to work nicely by setting the tag of all Children of EnemyFighter to "Untagged".

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 Supershandy · May 09, 2013 at 09:35 AM 0
Share

I feel a bit silly now......thank you so much for the help! :D

avatar image
0

Answer by Waz · May 06, 2013 at 02:46 PM

So you know the problem is in:

 var EnemySpeed = Target.GetComponent(EnemyAI).InterceptSpeed;

So either Target is null, or it has no EnemyAI component. That's the answer. Find out which it is, and fix that.

Debug like this:

 var targetAI = Target.GetComponent(EnemyAI);
 Debug.Log("targetai="+targetAI,this);

As you say, the scripts are the same. So look elsewhere for the problem. I suspect you've dragged the wrong object to 'Target'.

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 Supershandy · May 06, 2013 at 02:52 PM 0
Share

There is no object to drag to target as "target" is a private variable, it's automatically set by the AI using "GameObject.FindWithTag (""). I'll try the debug and see what comes up

avatar image Supershandy · May 06, 2013 at 02:59 PM 0
Share

All I get from the debug is

TargetAI UnityEngine.Debug:Log(Object, Object) FriendlyAI:Intercept() (at Assets/FriendlyAI.js:156) FriendlyAI:FixedUpdate() (at Assets/FriendlyAI.js:65)

Where fixedupdate line 65 is just Intercept()

avatar image Supershandy · May 06, 2013 at 07:26 PM 0
Share

I'm assu$$anonymous$$g there is going to be no solution to this problem, short of supplying the code

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to add variables to Components (Nav Mesh Agent)? 0 Answers

Problem with array on gameobjects 2 Answers

An instance of type 'UnityEngine.Component' is required to access non static member 1 Answer

How to Change Background Sprite at run time ? 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