So I’m making a multiplayer game for a Uni project and i have a buildingSystem script, where I need to set the main cam of the first person character to be able to see the building previews. The problem is, the players are spawned after the game ist started, so I cannot drag the camera from the scene to the script in the inspector. How could I get the camera to be added to the inspector after the game was started and the player spawned?
so this is my player prefab
and this is my BuildSystem game object where the buildSystem script is in.
This is fart of the buildSystem script:
private void DoBuildRay()//actually positions your previewGameobject in the world
{
Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);//raycast stuff
RaycastHit hit;
if(Physics.Raycast(ray,out hit, 100f, layer))//notice the layer
{
float y = hit.point.y + (previewGameObject.transform.localScale.y / 2f);
Vector3 pos = new Vector3(hit.point.x, y, hit.point.z);
previewGameObject.transform.position = pos;
@Frank07101997
You said you are making a multiplayer game so there will be a lot of Cameras with the same names so you should use “Transform.GetChild”.
Transform Camera;
void Start()
{
//Get the first child of the object in the hierarchy.
Camera = gameObject.transform.GetChild(0);
Debug.Log(Camera);
}
Attach this script on the player. Note that this script will get the first child of the object in the hierarchy so make sure the Camera is the first child. I hope this was helpful. This is my first answer here,You said you make a multiplayer game so the Camera names will be the same and that’s why I think you should use “Transform.GetChild”. Unity - Scripting API: Transform.GetChild
Transform Camera;
void Start()
{
//Get the first child of the object in the hierachy.
Camera = gameObject.transform.GetChild(0);
}
Attach this script to the player so make sure that the first child is the Camera. Note that it will get the first child of the object in the hierarchy. I hope this was helpful. This is my first answer here.
@Frank07101997
You said you are making a multiplayer game so there will be a lot of Cameras with the same names so you should use “Transform.GetChild”.
Transform Camera;
void Start()
{
//Get the first child of the object in the hierarchy.
Camera = gameObject.transform.GetChild(0);
Debug.Log(Camera);
}
Attach this script on the player. Note that this script will get the first child of the object in the hierarchy so make sure the Camera is the first child. I hope this was helpful. This is my first answer here
You can use Camera.main to get the main camera, it would look like this:
private Camera cam;
void Start()
{
cam = Camera.main;
}