2016-04-15 2 views
0

カメラオプションを設定できません。ゲームをして別のシーンを読み込むと、カメラのオプションが変わります。しかし、私は同じシーンのためにプレーをヒットしたとき、それは正しく動作します!コンソールは言った:MissingComponentException unity2D

MissingComponentExceptionを:そこには「カメラ」は 「キャンバス」ゲームオブジェクトに添付されていないが、スクリプトはそれにアクセスしようとしています。

おそらく、ゲームオブジェクト "Canvas"にカメラを追加する必要があります。 。またはあなたの スクリプトはここ

」それを使用する前に、部品が装着されているかどうかを確認する必要があります私の現在のスクリプトです:

using UnityEngine; 
using System.Collections; 

public class PixelPerfectCamera : MonoBehaviour { 

    public static float PixelsToUnits = 1f; 
    public static float scale = 1f; 

    public Vector2 nativeResolution = new Vector2(400, 160); 


    void Awake() 
    { 

     var camera = GetComponent<Camera>(); 


     if (camera.orthographic) 
     { 
      scale = Screen.height/nativeResolution.y; 
      PixelsToUnits *= scale; 
      camera.orthographicSize = (Screen.height/2.0f)/PixelsToUnits; 
     } 
    } 
} 

答えて

2

あなたはカメラが、同じに接続していないんPixelPerfectCameraスクリプトが接続されているゲームオブジェクト。

シーンで唯一のカメラがある場合は、変更

var camera = GetComponent<Camera>(); 
あなたのシーンで複数カメラを持っている場合

Camera camera = Camera.main; //Use the main camera 

には、使用:

Camera camera = GameObject.Find("NameOfGameObjectCameraIsAttachedTo").GetComponent<Camera>(); //Use the main camera 

あなたはカメラが接続されているゲームオブジェクトの名前にNameOfGameObjectCameraIsAttachedToを変更する必要があります。

0
この目的のために、スクリプトを変更し

using UnityEngine; 
using System.Collections; 

public class PixelPerfectCamera : MonoBehaviour { 

    public static float PixelsToUnits = 1f; 
    public static float scale = 1f; 

    public Vector2 nativeResolution = new Vector2(400, 160); 
    public Camera camera; 

    void Awake() 
    { 
     if (camera.orthographic) 
     { 
      scale = Screen.height/nativeResolution.y; 
      PixelsToUnits *= scale; 
      camera.orthographicSize = (Screen.height/2.0f)/PixelsToUnits; 
     } 
    } 
} 

次に、このスクリプトのカメラフィールドにシーンでお使いのカメラを取り付けます。

関連する問題