- Home /
Do scripts get cloned with prefabs?
Hello, a strange one:
I have a prefab 'Car' (just a Cube in fact) with a simple Motor script attached:
function Update(){
transform.Translate(Vector3.forward Thrust Time.deltaTime);
}
With one copy of the prefab in the scene, it works as expected. However with 2 or more, I noticed that apparently neither object was moving. By looking in Scene view at runtime I could in fact see that the transforms are moving (the local origin orthogonal axes move around the screen) but somehow the Cube meshes are being left in their original positions.
Any clues at all about what this could be?
When prefabs are cloned, their scripts (and internal references to transform, gameObject etc.) are cloned and updated too right?
Answer by Landern · Nov 18, 2013 at 04:58 PM
Yes, you should get the scripts as well when Instantiating another object of the same type. Can you post your Cloning/Instantiate function? Are you perhaps getting the wrong object in the Hierarchy?
Hi thanks for the answer.
I'm actually not instantiating these objects in code - I've just placed 2 copies of the prefab in the scene. Here's some detail if it helps:
Prefab name 'Car" Cube $$anonymous$$esh Standard $$anonymous$$eshRenderer Standard Rigidbody (using gravity, not kinematic) Standard Box Collider Vehicle$$anonymous$$otor.js script:
pragma strict
var Thrust : float = 3;
function Update () {
transform.Translate(Vector3.forward * Thrust * Time.deltaTime);
}
1) drag 1 prefab into scene; hit Play, object moves as expected
2) drag prefab a 2nd time into scene (from Prefabs folder); hit play; neither mesh moves. If I switch to Scene view with either object selected while game is running, I can see that the transform is moving.
Answer by pvpoodle · Nov 18, 2013 at 05:32 PM
I havnt encountered this peculiar problem you are facing.
But as a work around i would suggest you create an empty gameobject attach the character motor script you are using to the empty and then child the car mesh you are using under the empty. you can rename the empty and create a prefab from it.
this way the mesh will be moved since the parent gameobject is being moved.
idealy you should be instantiating the prefabs from script, which has always worked well for me.
Your answer
Follow this Question
Related Questions
Problems about prefabs and velocity affected by scripts 0 Answers
Objects added to scene through extended editor not drawn until selected. 2 Answers
Prefab, referencing them, using their methods:bug or intentional? 1 Answer
Making a cloned particle emitter fire once only 2 Answers
Can prefabs drop attached scripts? 0 Answers