The main camera is inside Player and I want to reference access the camera from the script on the object
Window Interaction Scene
The script is :
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Window_Interaction_Action : MonoBehaviour
{
private Camera playerCamera;
private List<GameObject> birds = new List<GameObject>();
private void Awake()
{
playerCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
}
public void RandomBirdCamera()
{
birds = GameObject.FindGameObjectsWithTag("Flocking Bird").ToList();
GameObject randomBird = birds[Random.Range(0, birds.Count)];
List<Transform> randomBirdChilds = randomBird.GetComponentsInChildren<Transform>().ToList();
foreach (Transform child in randomBirdChilds)
{
if (child.name == "head")
{
child.gameObject.AddComponent<Camera>();
playerCamera.enabled = false;
}
}
}
}
This screenshot is before running the game in editor mode :
Now I’m using Find before I just used a public Camera playerCamera;
The problem is when I’m running the game the Player will be on the scene : DontDestroyOnLoad and the object with the script will stay in the scene Game so it will give me error that the camera is missing :
This screenshot is after running the game :
Seems like GameObject.Find is not working between scenes.


