Unity 5 Is Trigger problem

Hi all, I’m new to unity so please be nice, it’s my first full day programming in Unity and using C# for that matter…:slight_smile:

I’m using Unity 5.1.03f Pro

I have a sphere (player) which fires across the scene to hit a target, if it hits it it remains stationary and the OnTriggerEnter on the target’s script destroys the target, a message is sent to a game controller to instantiate a new target at a random location ready for the ‘player’ to fire themselves at… it all works beautifully except that when the second target appears (a clone of the first) when the ‘player’ hits it nothing happens, I tried moving the ‘player’ over the target using the transformation sliders and nothing… no subsequent activation of the OnTriggerEnter code… I have been at this for about 6 hours now, searched every forum I could find and I’m at a loss… I have tried every work around I can think of…

I have read on certain forums that when a collider is ‘teleported’ or destroyed from within the IsTrigger collider that Unity has difficulties and the correct messages are not sent to activate OnTriggerEnter, Exit etc…

I originally had it so the target just relocated when hit but to try to work around this problem I set the code to destroy the target when hit and a separate gameobject to instantiate a new target prefab.

Here is the code from the target:

void Start(){

	//show target
	gameObject.GetComponent<Light> ().enabled = true;

	void OnTriggerEnter(Collider other) {

	//When the player hits the mesh, turn off gravity and freeze position
	other.gameObject.GetComponent<Rigidbody> ().useGravity = false;
	other.gameObject.GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezePosition;

	//Reset the x variable in the players script so that the player can 'jump' again
	GameObject player1 = GameObject.Find("Player1");
	Rotation1 rotation1 = player1.GetComponent<Rotation1>();
	rotation1.x = 0;

	//Reduce the light down
	gameObject.GetComponent<Light> ().intensity = .01f;

	//Set variable in GameController to true so a new mote is instantiated
	GameObject mastercon = GameObject.Find ("MainControl");
	GameController gameController = mastercon.GetComponent<GameController> ();
	gameController.moveMote = true;

	//Destroy target
	Destroy (gameObject);


And here is the code from the gamecontroller:

void FixedUpdate(){


		//Generate a random location
		Vector3 newPosition = new Vector3 (Random.Range (0.0F, 4.0F), 0, Random.Range (-2.5F, 4.0F));
		//instantiate a new target
		GameObject mote = Instantiate(Resources.Load("Sphere"), newPosition ,Quaternion.identity) as GameObject;

		moveMote = false;

I hope someone knows what I can do to make the OnTriggerEnter work after the first activiation…

I had to work around this one by creating multiple targets and having a new target appear each time instead of using a single prefab for them all, this has fixed the problem but it is a workaround and not a solution to the underlying issue.