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 ;
}
}
}