- Home /
OnTriggerEnter NullReferenceException
I'm trying to make a random dungeon generator by having pieces of the dungeon instantiate trigger colliders that, if there is no occupied space, will instantiate another dungeon piece, and then it goes on until it reaches a selected number of pieces. However, the trigger colliders aren't doing their job. The dungeons end up intersecting themselves in several places, when the trigger colliders are supposed to not create any objects if there is an object there, and also make the Corridor (the dungeon pieces that spawn more pieces) spawn something else, until it finds something that works. I believe it has something to do with this error that shows up when the dungeon generates:
"NullReferenceException: Object reference not set to an instance of an object TriggerTester.OnTriggerEnter (UnityEngine.Collider other) (at Assets/TriggerTester.js:35)" (It's referring to line 31 in the script below, it didn't transfer correctly and left out a few lines.)
Here's the script causing the error: #pragma strict
var dungeonPart : GameObject;
var dungeonMaster : GameObject;
var dungeonMasterScript : DungeonMaster;
function Awake ()
{
dungeonMaster = gameObject.Find("DungeonMaster");
dungeonMasterScript = dungeonMaster.GetComponent(DungeonMaster);
WaitAndDestroy();
}
function Update ()
{
}
function WaitAndDestroy()
{
yield WaitForSeconds(0.1);
var Child = Instantiate(dungeonPart, transform.position, transform.rotation);
Child.transform.parent = transform.parent.gameObject.transform;
Destroy(gameObject);
}
function OnTriggerEnter(other : Collider)
{
if (transform.parent != null && transform.parent.tag == "Corridor")
{
transform.parent.GetComponent(CorridorScript).hasSpawned = false;
}
if (transform.parent.parent != null && transform.parent.parent.tag == "Corridor")
{
transform.parent.parent.GetComponent(CorridorScript).hasSpawned = false;
}
if (transform.parent != null && transform.parent.tag != "Corridor")
{
Destroy(transform.parent.gameObject);
}
if (transform.parent.parent != null && transform.parent.parent.tag != "Corridor")
{
Destroy(transform.parent.parent.gameObject);
}
Destroy(gameObject);
}
Your answer
Follow this Question
Related Questions
Collision Detection not working properly 2 Answers
Whats wrong with my score script 1 Answer
how to make an Explosive rigidbody trigger by another collision box 0 Answers
Car dynamics? 0 Answers