Only detect collision once, if statement. C#

Hello everyone. I’m creating a simple endless runner game, and I’m having trouble creating a platform generator that generates platforms as the player goes further in the game. I wrote a script to randomly generate the y value of the next platform, and that works. The thing I’m having trouble with is generating the x value. I tried to do something like adding 77 units to the position of the next platform instantiate when it hits a previous platform. The problem is, when I do this, I get an endless amount of platforms generated as long as the player is in contact with the previous platform. This ends up in a mass of platforms and eventually crashing the game. How would I detect only one collision and not accept any more after that one?

By the way, I’m using a prefab as my platform.

Here’s my script:
using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {

	private Transform player;
	public float timer;
	public float playerSpeed;
	private bool touchingPlatform;
	public float acceleration;
	public Vector3 jumpVelocity;
	public float distanceTraveled;
	public GameObject Floor;
	private Vector3 platformPosition;
	int x,y;
	// Use this for initialization
	void Start () {

		player = transform;
		player.position = new Vector3 (0, 0, 0);
		timer = Time.time;
		y = Random.Range (-3, 3);
		platformPosition = new Vector3 (x,y, 0);
	// Update is called once per frame
	void Update () {

		player.Translate (Vector3.right * Time.deltaTime * playerSpeed);
		if (touchingPlatform && Input.GetButtonDown("Jump")) {
			rigidbody.AddForce(jumpVelocity, ForceMode.Acceleration);
			Debug.Log("Space was pressed");
		distanceTraveled = player.localPosition.x;

	void FixedUpdate() {

		if (touchingPlatform) {
			rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);

	void OnCollisionEnter (Collision playerCollide) {
		if (playerCollide.gameObject.tag == "Firstplat") {
			Instantiate(Floor, platformPosition, Quaternion.identity);
		if (playerCollide.gameObject.tag == "Floor") {
			x = x + 77;
			Instantiate(Floor, platformPosition, Quaternion.identity);

		touchingPlatform = true;

	void OnCollisionExit () {

		touchingPlatform = false;


Because of you’re Instantiating Objects as long as trigger enter with that Object, so obviously it’ll generate multiple times.

private string lastCollidedObjName = "";
private bool generatePlatform = false;

void OnCollisionEnter(Collision obj)
if(!generatePlatform )
//Instantiating whatever objects you want, it'll instantiate only one time
lastCollidedObjName =;
generatePlatform = true;

void OnCollisionExit(Collision obj)
if(generatePlatform && == lastCollidedObjName )
generatePlatform = false;