Reputation: 89
i have 2 camera in my 2D scene. i want to change the player camera to another one. but it gives me error
here is the error:
MissingComponentException: There is no 'Camera' attached to the "player" game object, but a script is trying to access it.
You probably need to add a Camera to the game object "player". Or your script needs to check if the component is attached before using it. SwitchCamera.Start () (at Assets/scripts/SwitchCamera.cs:10)
this is my code:
public Camera camera1;
public Camera camera2;
void Start () {
//camera2 = GameObject.Find("Player").GetComponentInChildren<Camera>();
camera1 = transform.FindChild("player").gameObject.camera;
}
void Update()
{
if (Input.GetKeyDown (KeyCode.C)) {
camera1.GetComponent<Camera> ().enabled = true;
//camera2.GetComponent<Camera>().enabled = true;
}
}
Upvotes: 0
Views: 6710
Reputation: 1
I believe that message means you need to drag and drop your Camera into the inspector.
The next issue you're going to have is the
public Camera cam1;
is referencing a camera component, not a Camera Objects. So when you try:
cam1.enabled = false;
You're leaving the audio-listener component on, and turning off the camera component (which will throw another warning message).
The solution:
public GameObject cam1;
cam1.SetActive(false);
which will "disable" the Camera Object (all the components, including the audio-listener components and camera components)
Upvotes: 0
Reputation: 164
In answer to your question I don't see function FindChild in documentation of Transform. I think you should use Find function: http://docs.unity3d.com/ScriptReference/Transform.Find.html You can use this simple code to try find problem.
void Start () {
GameObject go = transform.Find("player").gameObject;
if (go == null){
print("GameObject not found");
}else{
Camera camera1 = go.GetComponent<Camera>();
if (camera1 == null){
print("Camera not found");
}
}
}
If you will get "Camera not found" maybe you didn't add Camera component to GameObject "player" or Camera Component is destroyed by other Script or Camera component don't exist yet, because you add camera after run this script. I dont't know all scripts and hierarchy of gameobjects and components in project.
Upvotes: 0
Reputation: 3082
First of all, as for current Unity3D version:
Property camera has been deprecated. Use GetComponent() instead. (UnityUpgradable)
as stated in API docs.
Also, if you need to switch between cameras, one way would be to assign them from inspector and then go for enabled
property.
Other way round is finding it in scene (quite inefficent if you don't know exact location of it). I would recommend creating some sort of service or camera manager that would be a single point of changing cameras. Remember that cameras work for all your scene, not for certain object only (but they can use culling to render only exact layers you want).
Example:
public Camera camera1;
public Camera camera2;
public void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
//Swap enabled state to opposite one provided that only is on at a time
camera1.enabled = !camera1.enabled;
camera2.enabled = !camera2.enabled;
}
}
camera1
and camera2
should be assigned from inspector, that is drag and drop GameObject
with Camera
component attached to the field of script inspector (first camera to camera1 field and second camera to camera2 field and so on).
Upvotes: 1