Hello everybody,
I have a prefab which has a 3D model + a camera. Plus, I have my main camera in the scene. So, when I load the prefab up, its camera becomes the current active camera, but I just want the main camera taking that camera’s position and trasform and I don’t what the prefab’s camera be active. How can I retrieve the prefab’s camera once it is uploaded? it it
.GetComponent(“camera”) enough?
many thanks.
Giancarlo
What you really want to put on the prefab is not another camera - in stead you want to be able to easily specify where the camera should be placed in relation to the object.
So on your prefab, remove all components from the camera GameObject and drag-drop it to a public variable on a script on the root of the prefab of type Transform.
Now, when you instantiate your prefab, add the following as well:
// Somewhere above, your freshly created instance is assigned to a variable named newInstance
// The name of the script on the root of the prefab is CharacterScript and the public Transform variable holding the reference to the camera transform is called cameraTransform
Camera.main.transform.parent = newInstance.GetComponent<CharacterScript> ().cameraTransform;
Camera.main.transform.localRotation = Quaternion.identity;
Camera.main.transform.localPosition = Vector3.zero;
uhm I’m doing it in Javascript. In my Project directories Ihave all my prefabs in the “Resources” directory and then I call
var gg: GameObject = Instantiate (“/rooms/myroom”, GameObejct); to create a new room.
so after that you call should I add the 3 statements you told me?
Thannks
GC.
Assuming you have set up your prefab and script as I described before the code segment, yes. This is the same code segment translated to JS:
Camera.main.transform.parent = gg.GetComponent (CharacterScript).cameraTransform;
Camera.main.transform.localRotation = Quaternion.identity;
Camera.main.transform.localPosition = Vector3.zero;
So … let’s me see if I’ve understand:
I have my prefab with a script (let’s call the script A). In A, I have a public variable which is a Transform.
in the main Scene: I have a main Camera and a StartCamera. The start Camera is not active, and I drag&Drop it onto the script attached to the prefab.
Once (in another script) I called the Istantiate of that prefab I can call your three statements. So, now this is my question. I can Drag&Drop the camera is my scene starts with THAT prefab already in the scene. But Unity doesn’t allow me to drag and drop anything on a script (which is attached to a prefab not yet allocated
) … I guess…
dunno if it really clear.
Nope, errata corrige… I can do it! 
uhm… doens’t work
as I was expecting.