OnTriggerEnter2D fires multiple times on play

Hi there

I have an issue, which I do not understand at all and I can find no one who seems to have the same problem. I have a game created in Unity using the 2d components with c# as the main scripting language.

The set up is as follows. I have a playable character, which gets a vector2 on Input.GetMouseButtonDown(0), that it then moves too and I have a camera controller which gets its vector3 from that same function. To detach the camera from the character and make an end of the scene, I have created two sets of triggers at either end of the scene, which will detach the controller from the camera. This way I have an end to the scene and the character is unable to move any further.

My problem is this: every so often, when I hit play, the trigger will fire non stop. This is not something that happens every time I hit play, but just once in a while and it is driving me up the wall.

this is my code:

on the player:

public class charCtrl : MonoBehaviour {

	public float speed;
	
	Vector2 target;
	
	public cameraCtrl CamCtrl;
	
	void Start () {
		
		target = transform.position;

		CamCtrl = FindObjectOfType <cameraCtrl>();
	}
	
	void Update () {

		if (Input.GetMouseButtonDown(0) && !stopHer) {
			target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			target.y = -0f;
		}

		CamCtrl.getMeThere = target;
		CamCtrl.camSpeed = speed;

		transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
		
	}

	public void stopTheChar (){

		target = transform.position;

	}
}

this is the border:

public class sceneBorder : MonoBehaviour {

	public cameraCtrl mainCamera;

	bool camMotion;

	public bool inOut;

	// Use this for initialization
	void Start () {
	
		mainCamera = FindObjectOfType<cameraCtrl>();

	}

	public void OnTriggerEnter2D(Collider2D Fupa){

		Debug.Log ("is Triggered");

		if (inOut) {
			camMotion = false;
			stopCam ();
		} else if (!inOut) {
			camMotion = true;
			stopCam ();
		}
	}

	void stopCam(){
		mainCamera.cameraStill = camMotion;
	}
}

and this is on the camera:

public class cameraCtrl : MonoBehaviour {

	public Vector2 getMeThere;
	
	Vector3 realPos;
	
	public float camSpeed;

	public bool cameraStill;
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		if(!cameraStill){
			movement();
		}else {

		}
	}

	void movement(){
		realPos.Set(getMeThere.x, getMeThere.y, -10.0f);
		transform.position = Vector3.MoveTowards(transform.position, realPos, camSpeed * Time.deltaTime);
	}
	
}

I have tried everything I can think of, but it keeps on happening. If anyone has had a similar experience and knows of a way that I can fix it please let me know and sorry for the long post

Since you’re not checking what is triggering the trigger, it is most likely some physics object rolling in and out of your trigger. In OnTriggerEnter2D(Collider2D collider), use the collider variable to get the info of what entered your trigger, and only perform the actions if the expected object is in the trigger.

void OnTriggerEnter2D(Collider2D coll)
{
  if(coll.GetComponent<charCtrl>() == null) //Checks if the game object that entered is your character by looking for the charCtrl script. Alternatively, you could use tags.
    return;

  //Your code goes here.
}

Just add a tag to the object that should enter the trigger. Than in the OnTriggerEnter() function add an if statement making sure that the only thing that makes the code run is if it is the proper object setting off the trigger.