- Home /
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?
$$anonymous$$y guess is that this line is failing somehow.
var Target : Transform = target.transform;
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
Have you verified that lowercase target is indeed set? If not that should give you a good starting point for tracking the problem.
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;
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.
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".
I feel a bit silly now......thank you so much for the help! :D
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'.
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
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()
I'm assu$$anonymous$$g there is going to be no solution to this problem, short of supplying the code