- Home /
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 ;
}
}
}
I think you need to try to make this more clear. Your CreatePrefab() function is really confusing. Can you add some comments to the script to explain why you are doing each line? What is the purpose of gObj and gNew? Why are you setting it, and then immediately destroying it in CreatePrefab()?
createPrefab fn is used to assign the new gameobject to the old gameobject and destroy the old gameobject and instantiate the new at its place..
gObj holds the old gameobject and gNew holds the new gameObject.
Basically, i want to destroy the gameobject on touch and instantiate the same gameobject in the touch position..
It looks like you aren't referencing any prefab. In your start function, you're putting the player object in the scene into gObj and gNew. So, you now have no reference to any prefab that you might have had. All you're doing is duplicating the object in the scene, not instantiating from a prefab.
Thanks, but could you explain me a more what did you mean by duplicating the object and not instantiating from prefab . Didn't Instantiating prefab, duplicates prefab in the scene and also when you instantiate a prefab , you can clearly see the prefab name changes into prefab(clone)..
Also can you give me the solution, if I am wrong and how could i have done better..
Answer by rockyourteeth · Oct 13, 2015 at 03:09 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612041025im_/https://answers.unity.com/themes/thub/images/avi.jpg)