- Home /
Null Reference Exception error
Hi guys
new to C# (Kinda) Getting a null reference error on Line 22 (ill post the full code below, but you can see the error and line 22 in the image, where the error is pointing to. any ideas on how to fix? Its baffled me.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SF_PatrolEnemyMove : SF_Enemy_Move
{
//list to hold waypoinmts for movement
public List<Vector3> mLS_V3_Waypoints = new List<Vector3>();
//Holds indec in mLS_V3_Waypoints that is current target
private int mIN_TargetIndex = 0;
//Holds all colliders that the enemy vision collider hits
public List<Collider> mHitTargets = new List<Collider>();
// Use this for initialization
void Start ()
{
base.Start ();
mST_State = "patrol";
mTM_StateText.text = mST_State;
HP = 20;
Defence = 8;
}
//initialise patrol path for enemy
public void StartPatrol(List<Vector3> vLS_V3_Waypoints, float vFL_Speed)
{
mFL_Speed = vFL_Speed;
foreach(Vector3 tV3_Waypoint in vLS_V3_Waypoints)
{
mLS_V3_Waypoints.Add (tV3_Waypoint);
}
mST_State = "patrol";
mTM_StateText = GetComponentInChildren<TextMesh> ();
mTM_StateText.text = mST_State;
mV3_TargetPos = mLS_V3_Waypoints [0];
}
//method to switch states of enemy by triggers
protected override void Triggers()
{
if(mMB_PCScript.ST_State == "carry" && mST_State != "flee")
{//change to flee state if PC is in carry
mST_State = "flee";
mTM_StateText.text = mST_State;
}
if(mMB_PCScript.ST_State == "walk" && mST_State == "flee")
{//change to chase state if PC is in walk state and NPC was just fleeing
mST_State = "chase";
mTM_StateText.text = mST_State;
}
if(mST_State == "chase" && mFL_TargetDistance > 10F)
{ //change to roam state id PC is outside of a certain range
mST_State = "patrol";
mTM_StateText.text = mST_State;
}
}
//---------------TRIGGERS------------
void OnTriggerEnter(Collider vCL_Spotted)
{
//if collider is not already in mHitTargets List and not
//tagged ignore like our waypoint markers
if(mHitTargets.Find(x => x == vCL_Spotted) == null && vCL_Spotted.transform.tag != "ignore")
{
mHitTargets.Add (vCL_Spotted);
}
//createt a collider to hold closest object we collide with
Collider tCL_Closest = null;
//variable to store closest distance
float mFL_ClosestDis = 99;
//loop through all colliders in list
// foreach(Collider col in mHitTargets)
// {
// if(col.transform!=null)
// {
// //if distance between object collided with is less than previous distance of currrenly closest object then update
// if(Vector3.Distance (col.transform.position, transform.position) < mFL_ClosestDis)
// {
// mFL_ClosestDis = Vector3.Distance(col.transform.position, transform.position);
// tCL_Closest = col;
// }
// }
// }
//if closest object is not null this only causes problems if object collided with is tagged ignore
if(tCL_Closest != null)
{
if(tCL_Closest.gameObject.tag == "Player")
{//if player is closest object then chase
mST_State = "chase";
mTM_StateText.text = "!";
mTM_StateText.color = Color.red;
}
}
tCL_Closest = null;
mFL_ClosestDis = 99;
}
//Method called on the frame an objct stops colliding with the enemy
void OnTriggerExit(Collider vCL_Spotted)
{
mHitTargets.Remove (vCL_Spotted);
}
//Our method to called state movement methods
protected override void Move()
{
base.Move ();
if (!mLM_LevelManager.isPaused())
{
//resolve game states
switch (mST_State)
{//Call appropriate methods to deal with movement in current state
case "patrol": Patrol(); break;
}
}
}
//method defining movement in patrol state
void Patrol()
{
if (Vector3.Distance (mV3_CurrentPos, mGO_Character.transform.position) > 0.1F)
{
mBL_Moving = true;
//find the vector pointing from our position to the target
Vector3 tV3_Direction = (mV3_CurrentPos - mGO_Character.transform.position).normalized;
//create the rotation we need to be in to look at the target
Quaternion tQT_Rotation = Quaternion.LookRotation(tV3_Direction);
mBL_Moving = true;
//Rotate gradually to target instead of instantly
mGO_Character.transform.rotation = Quaternion.Lerp (mGO_Character.transform.rotation,
tQT_Rotation, 15 * Time.deltaTime);
mGO_Character.transform.position = Vector3.MoveTowards (mGO_Character.transform.position,
mV3_CurrentPos, mFL_Speed * Time.deltaTime);
}
else if(Vector3.Distance (mV3_TargetPos, mGO_Character.transform.position) > 0.1F)
{
ChooseNextPosition();
}
else
{
transform.position = mV3_TargetPos;
}
}
//Method to set movement target for all states
protected override void SetTarget()
{
//patrol state set target
if (mST_State == "patrol")
{
if(mIN_TargetIndex == 0)
{
SetPatrolTarget(mLS_V3_Waypoints[0]);
}
else
{
SetPatrolTarget(mLS_V3_Waypoints[mIN_TargetIndex]);
}
ComputeTargetDistance();
//if the NPC has reached the target waypoint choose another
if (mFL_TargetDistance < 0.1F)
{
if(mIN_TargetIndex == (mLS_V3_Waypoints.Count - 1))
{//if reached end of list then choose the first waypoint again
SetPatrolTarget(mLS_V3_Waypoints[0]);
mIN_TargetIndex = 0; //update index
}
else
{
SetPatrolTarget(mLS_V3_Waypoints[mIN_TargetIndex + 1]);
mIN_TargetIndex++; //update index
}
}
}
else
{//all other states use base class settarget()
base.SetTarget();
}
}//--------------
void SetPatrolTarget(Vector3 vV3_Pos)
{
mV3_TargetPos = vV3_Pos;
}
}
Answer by Halfbiscuit · May 18, 2015 at 10:58 AM
If you look further down at line 34,35 and 36 you can see this:
mST_State = "patrol";
mTM_StateText = GetComponentInChildren ();
mTM_StateText.text = mST_State;
'mTM_StateText' is being initialised at line 35 but in your start function you forgot to do this.
mTM_StateText.text cannot be set because mTM_StateText is currently null.
Answer by Cherno · May 18, 2015 at 10:58 AM
I don't see neither mST_State nor mTM_StateText being declared anywhere.
Answer by MoonwalkingBaz · May 18, 2015 at 11:09 AM
mST_State = "patrol"; mTM_StateText.text = mST_State;
Both variables are being declared in the start function, line21 + 22
Well no they are not being declared on those lines, they are being set. I am assu$$anonymous$$g the variables were declared in the parent class: SF_Enemy_$$anonymous$$ove.
yes they are
sorry, im a newbit at unity,, how would i fix it?
If you look at the answer I gave above you need this line(or any line that sets mT$$anonymous$$_StateText):
mT$$anonymous$$_StateText = GetComponentInChildren ();
before you set the text variable in line 22.
and should i put that where all the other variables are being initialised or within a function?
Basically change your start function to this:
void Start ()
{
base.Start ();
mST_State = "patrol";
mT$$anonymous$$_StateText = GetComponentInChildren();
mT$$anonymous$$_StateText.text = mST_State;
HP = 20;
Defence = 8;
}