LoginSignup
1
0

More than 5 years have passed since last update.

unityにおいてボタンでカメラの切り替えを行う。

Last updated at Posted at 2019-01-23

Unity 2018.3.1f1を使い始めた初心者です。
シーンに配置した二つのカメラ"MainCamera"と"SubCamera"をButtonで"MainCamera"から"SubCamera"へ切り替わるようにしたいのですが
NullReferenceException: Object reference not set to an instance of an object
のエラーが出てしまいます。

使用しているスクリプトは以下のものです。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class switchcamera : MonoBehaviour { 

Camera mainCamera; 
Camera subCamera;

public switchcamera()
{
}

void start(){ 
    mainCamera = GameObject.Find("MainCamera").GetComponent<Camera>(); 
    subCamera = GameObject.Find("SubCamera").GetComponent<Camera>(); 
    subCamera.enabled = false;
    mainCamera.enabled = true;
}
public void OnClick() { 
    mainCamera.enabled = false; 
    subCamera.enabled = true; 


} 

}

ヒエラルキー画面はこのような要素になっています。
スクリーンショット 2019-01-24 1.53.41.png

MainCameraのセッティング画面です。
スクリーンショット 2019-01-24 1.56.47.png

SubCameraはこちらです。
スクリーンショット 2019-01-24 1.57.39.png

Buttonのinspectorはこちらです。
スクリーンショット 2019-01-24 2.00.30.png

findがうまくいっていないのかと思っています。
どなたか、ご教授お願いいたします。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0