Two triggers overlapping, only activating ontriggerenter sometimes.

Hey! I’m making a game with procedurally generating roads. its all working pretty well, but I have to get them to stop overlapping one another.

to do this I’ve put a trigger with a rigidbody(no gravity, is kinematic) on each road section. the idea is that if the triggers touch, they’ll activate ontriggerenter and I can then have the program fix the overlap. However, ontriggerenter only fires some of the time.

I’m also getting a weird “object reference not set to an instance of an object” error over and over. I’ve checked the line in question and I think everything should be working. I’ve prepared some images to show you the problem in more detail. does anyone know what the problem could be? Thanks for your help!
[38476-use+triggers+to+detect+overlaps.jpg|38476]
[38477-mysterious+error±+very+related.jpg|38477]

And here’s the code in question:

using UnityEngine;
using System.Collections;

public class RoadStartstuff : MonoBehaviour
{
	GameObject Director; 
	GameObject parent;
	public int degreeturn; 
	public int degreessofar =0;
	public GameObject beforeme;
	// Use this for initialization

	
	void Start () 
	{
		Director = GameObject.Find ("Director");
		parent = this.transform.parent.gameObject;   //this gets the parent of this road start so we can filter out self collisions later...

	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}


	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.transform.parent.gameObject == parent)
		{
			//do nothing, you're colliding with yourself idiot.
			Debug.Log ("self collision. ignore.");
		}//end if
		else
		{
			//Debug.Log(Director.GetComponent<Director>().OMGoverlap);
			Director.GetComponent<Director>().OMGoverlap = true;  //<-----this is line 38 in monodevelop
			Debug.Log ("OMG WE HAVE AN OVERLAP BRUH! " + other.gameObject.transform.parent.gameObject.name);
			//here we also want to remember which type of road section was used when we collided so we can check later if all road sections have been tried.
			//we'll use a new array in the director that matches the protoroadsection array to keep track
			//once all possible road sections are tried, just cap it with a really slim special cap secion.
		}//end else
	}

}

OnTriggerEnter() only activates when at least one of the objects move

In this case, since none of them move, but are simply instantiated into place, then OnTriggerEnter() will never activate, effectively stopping your fail-safe code.

The way to fix this is to simply use this code on the generated road AFTER the objects instantiate

myTransform.position = myTransform.position + Vector3.zero;

This forces the physics system to respond, and even though it did not actually move, it processes the colliders and notices the interaction. This will then trigger your OnTriggerEnter() script. I cannot recommend where to place this code since I can’t see the code you use to instantiate the road.

Hope this helps :slight_smile:

OMG. I think i fixed it. in the script roadstartstuff where the flag was being set, director wasn’t a global variable, so ontriggerenter couldn’t see it, and thought it wasn’t assigned.

I’m a dope. I’m getting the proper number of debug messages now (they’re not firing at the right time, though. only after all the roads are built… so, yay! more problems.)

But, thanks for your help StrikeMasterz!