A little context… I am working on a multiplayer game and when the user logs in I want to load a model for their character and then I want to attach the main camera to the user’s model. I was able to create a prefab of the model and just have the camera disabled unless it’s a player being added to the scene. However I was hoping there was a way I can just take a prefab of the player camera and attach it through a script?
Thanks in advance for the help/advice.
You can attach a component to a gameobject using AddComponent.
You can the use the reference for changing values on the component.
Example:
Camera addedCamera = gameObject.AddComponent<Camera>();
According to the comment you gave to @nullgobz 's answer, you wan’t to attach the prefab of a camera to your characters. Then, you simply have to instantiate the prefab. The following code must be included in the character’s script.
// C#
// Drag & drop the prefab from the inspector
public GameObject CameraPrefab ;
private GameObject camera ;
void Start()
{
camera = (GameObject) GameObject.Instantiate( CameraPrefab ) ;
camera.transform.SetParent( transform ) ;
camera.transform.localPosition = Vector3.zero ;
camera.transform.localRotation = Quaternion.identity ;
...
}