scripts gets disabled when the prefab is instantiated

I have two script named inst and coll and two gameObject , one is empty and the other is the instance of prefab named Car . inst script is attached to the empty gameObject and its function is to destroy the car gameObject when touched and instantiate the car gameObject to the touch position.

coll script is attached to the car prefab , and its function is to detect collision.

script is working absolutely fine but a problem occurs. Whenever the instance is created of the car gameObject ,when touched ; The coll script attached to it get disabled in the created instance(clone) of the car.

I searched many sites , but was not able to clearly know the problem’s solution…

Here is my Inst script code

using UnityEngine;
using System.Collections;

public class inst : MonoBehaviour {

	Ray ray;
	RaycastHit hit;
	public GameObject gObj;
	public GameObject gNew;


	void Start () {
		gObj = GameObject.FindGameObjectWithTag("Player");
		gNew = GameObject.FindGameObjectWithTag("Player");
	}
	
	// Update is called once per frame
	void Update () {
		CreateInstance();
		
			}
	
	void CreateInstance (){
	
		if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
		
			
			ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

			Debug.DrawRay(ray.origin, ray.direction * 20);
			//Debug.Log(Physics.Raycast(ray,out hit, Mathf.Infinity));
			
			if (Physics.Raycast(ray,out hit, 20f))
				
			
			{
				
			
				CreatePrefab();
			}

				
				}
				
			}
		}
	
	}
	
	private void CreatePrefab() {

		gObj = gNew;
		if (gObj != null ){
			Destroy(gObj);
			gNew = Instantiate(gNew,hit.point, transform.rotation) as GameObject ;
		}

	}
	
}

First of all, terminology is confusing, so let’s make sure we’re on the same page. A “prefab” is an object which is not in the scene, but is ready to be created, or, instantiated into the scene. You have to keep a reference to that prefab through a variable that you set in the editor before you run the game. An “instance” of a prefab is one that you have instantiated, or created, in the scene. It is now an object in the scene, an instance of the prefab, but not a prefab anymore. But you should still keep your reference to the original prefab, so you can create more if you want to.

If you create an instance of a prefab, and then continue to duplicate the instance after that, instead of the prefab, you’re just making copies of copies, instead of making a bunch of copies from the original. This could result in unexpected behavior.

You probably want to create a “public GameObject CarPrefab”, or something like that, and place the prefab into that in the editor before you run the game. Then, during the game, you don’t want to set “CarPrefab” as anything new. Just leave that as the prefab reference, and use that when you instantiate:

gNew = Instantiate(CarPrefab, hit.point, transform.rotation) as GameObject ;

Now, gNew is always the new object instance, but CarPrefab always remains as a reference to the prefab.

And in this case, you should need “gObj” and “gNew”. Just use one of them. You can Destroy gObj before you instantiate a new object, and then set gObj as that new instance. Like this:

if (gObj != null )
{
  Destroy(gObj);
}
gObj = Instantiate(CarPrefab, hit.point, transform.rotation) as GameObject ;

Hope that helps.