LogicProblem_Cannot Destroy

[35538-run_test.zip|35538]

==================================================

At16:25

i did something success, but someing fail

GenAndDesGround Scripts:

using UnityEngine;
using System.Collections;

public  class GenAndDesGround : MonoBehaviour {

	public GameObject gGameController ;
	public GameController GameController ;
	public GameObject gGround;

	void Start () {

		gGameController = GameObject.FindWithTag("GameController");
		GameController = gGameController.GetComponent<GameController>();

	}


	void OnTriggerEnter(Collider other){

		GameObject Generate = Instantiate (gGround, new Vector3 (0, 0, 18), transform.rotation) as GameObject;
		Debug.Log ("Gen");
		GameController.gNewGround.transform.parent = Generate.transform;
		Destroy (GameController.gOldGround);
		GameController.gOldGround = GameController.gNewGround;
		GameController.gNewGround = Generate;
	}
}

For GameController Scripts:

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

	public GameObject gNewGround;
	public GameObject gOldGround;

	// Use this for initialization
	void Start () {

		gOldGround = GameObject.FindGameObjectWithTag ("YellowGroundOld");
		gNewGround = GameObject.FindGameObjectWithTag ("YellowGroundNew");
		gOldGround.transform.parent = gNewGround.transform;

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

		if(gNewGround!=null)
		{
			gNewGround.transform.Translate (Vector3.back*Time.deltaTime * 5);
		}
	}
}

Now, the problem is the generate position is keep different.

First Time generate, there are no space between OldGround and NewGround

After Trigger again and instaniate, there are space between OldGround and NewGround

After that , generate again, there are no space between OldGround and NewGround

…loop…

Can anyone help?

If I had to do it then in OnTriggerEnter I’ll make it to be:

void OnTriggerEnter(Collider other){
     // Place your old ground with your new ground object which means your new ground is now old ground
     GameController.gOldGround = GameController.gNewGround;
     // Instantiate a new piece of ground
     GameObject Generate = Instantiate (gGround, new Vector3 (0, 0, 18), transform.rotation) as GameObject;
     // Set your new ground to be this generated piece of ground
     GameController.gNewGround = Generate;
     // Set your old ground to be a child of new ground object
     GameController.gNewGround.transform.parent = Generate.transform;
 }