- Home /
This question was
closed Sep 19, 2014 at 10:45 AM by
Graham-Dunnett for the following reason:
Duplicate Question
Question by
ShadowArkitecht · Sep 19, 2014 at 10:44 AM ·
collisionerrornullreferenceexception
Using GetComponent Within OnTriggerEnter
I am having a problem with my code and the error NullReferenceException: Object not set to an instance of an object within the script Sensor. Which is below:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Sensor : MonoBehaviour
{
private Faction factionType;
private List<GameObject>mUnitsInSensor = new List<GameObject>();
public List<GameObject>UnitsInSensor
{
get{return mUnitsInSensor;}
set{mUnitsInSensor = value;}
}
void Awake()
{
factionType = GetComponentInParent<Faction>();
mUnitsInSensor.Clear();
}
void OnTriggerEnter(Collider other)
{
Faction otherFaction = other.gameObject.GetComponent<Faction>();
if(otherFaction.GetFaction != factionType.GetFaction)
mUnitsInSensor.Add(other.gameObject);
}
void OnTriggerExit(Collider other)
{
mUnitsInSensor.Remove(other.gameObject);
}
}
The line which is returning the error is the if statement within OnTriggerEnter. However the faction script is attached to every single object within the scene and the value set within the inspector.
using UnityEngine; using System.Collections;
public class Faction : MonoBehaviour { public enum mFaction { TERRAN_EMPIRE, SALAKI_EMPIRE }
public mFaction UnitFaction;
private mFaction mUnitFaction;
public mFaction GetFaction
{
get{return mUnitFaction;}
}
void Awake()
{
mUnitFaction = UnitFaction;
}
}
Therefore I do not understand why it is returning this error exactly. Any help would be greatly appreciated!
Comment