2016-12-13 4 views
2

自宅のマシンと大学のマシンに、異なるバージョンのUnityがインストールされています。私の家では新しく、大学では年上で、それが私の問題を引き起こしたのだろうかと思います。ゲームは私の家のコンピュータでさらに開発しようとするまでうまくいった。UnityEngine.Componentに定義が含まれていません... C#

'UnityEngine.Component' does not contain a definition for 'bounds' and no extension method 'bounds' of type 'UnityEngine.Component' could be found. Are you missing an assembly reference?

そして:

は、2つのエラーメッセージを取得

'UnityEngine.Component' does not contain a definition for 'MovePosition' and no extension method 'MovePosition' of type 'UnityEngine.Component' could be found. Are you missing an assembly reference?

をここに私のコードです:

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

public class SantaBagController : MonoBehaviour { 

    public Camera cam; 

    private float maxWidth; 

    // Use this for initialization 
    void Start() { 
     if (cam == null) { 
      cam = Camera.main; 
     } 
     Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f); 
     Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner); 
     float SantaBagWidth = renderer.bounds.extents.x; 
     maxWidth = targetWidth.x - SantaBagWidth; 
    } 


    // Update is called once per frame 
    void FixedUpdate() { 
      Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition); 
      Vector3 targetPosition = new Vector3 (rawPosition.x, 0.0f, 0.0f); 
      float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth); 
      targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z); 
      rigidbody2D.MovePosition (targetPosition);  
    } 
} 

助けてください!どうもありがとう!代わりに "MonoBehaviourのプロパティ"(な変換など、レンダラーが、...減価償却される使用

+0

その後、GetComponent<Renderer>().bounds.extents.x;

とレンダラを取得するためにコード全体をGetComponentを使用'Rigidbody2D meRigidbody = GetComponent ()'のようなコンポーネントのメンバであれば、正しい 'Component'型を使用していることを確認できます。 –

+0

'rigidbody2D'変数はどこにありますか?どのタイプですか? –

答えて

3

GameObjectディレクトリに接続されているコンポーネントにアクセスできなくなりましたあなたが過去にできたような穏やかな気分です。 は現在でなければなりませんGetComponentを使用してください。あなたのコードはユニティ4で有効であり、以下ではなく、5

でこれらのエラーです:

rigidbody2D.MovePosition(targetPosition); 

float SantaBagWidth = renderer.bounds.extents.x; 

Rigidbody2D rigidbody2D;rigidbody2Dを宣言し、それを修正します。私はいくつかのローカルを作成するために推薦するプロジェクトがユニティの古いバージョンと新しいバージョンで動作します、100%確かに

public Camera cam; 

private float maxWidth; 

Rigidbody2D rigidbody2D; 

// Use this for initialization 
void Start() 
{ 
    rigidbody2D = GetComponent<Rigidbody2D>(); 

    if (cam == null) 
    { 
     cam = Camera.main; 
    } 
    Vector3 upperCorner = new Vector3(Screen.width, Screen.height, 0.0f); 
    Vector3 targetWidth = cam.ScreenToWorldPoint(upperCorner); 
    float SantaBagWidth = GetComponent<Renderer>().bounds.extents.x; 
    maxWidth = targetWidth.x - SantaBagWidth; 
} 


// Update is called once per frame 
void FixedUpdate() 
{ 
    Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition); 
    Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f); 
    float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth); 
    targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z); 
    rigidbody2D.MovePosition(targetPosition); 
} 
+0

いくつかの5.xバージョンでは、古い参照と同じ命名の変数を使用すると、愚かな警告が発生することに注意してください。したがって、rigidbody2Dを使用すると、同じ名前の参照が非表示になることがあります。それはありません... – Everts

+0

はい、そうです。それらは初期のUnityバージョンでした。彼らは長い間去っており、私はOPが少なくとも5.3以上のバージョンを使用していると仮定します。そうでない場合は、OPにあなたのコメントが表示されます。私はまたこれが起こると信じている** **古いプロジェクトをインポートするときには5.xのバージョンをクリックして、Unityが古いAPIを使うべきかどうかを尋ねるときにYe​​sをクリックします。 – Programmer

+0

本当にありがとう! –

1

GetComponent

Renderer r = GetComponent<Renderer>(); 
    if(r != null) 
    { 
      float santaBagWidth = r.bounds.extents.x; 
      maxWidth = targetWidth.x - santaBagWidth ; 
    } 

あなたの剛体に同じアドバイスを明示的な機能を使用しています。それをキャッシュAwake関数で使用して固定アップデートで使用する

private Rigidbody2D r2D ; 

void Awake() 
{ 
    r2D = GetComponent<Rigidbody2D>() ; 
    if(r2D == null) 
     r2D = gameObject.AddComponent<Rigidbody2D>(); 
} 

void FixedUpdate() { 
    Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition); 
    Vector3 targetPosition = new Vector3 (rawPosition.x, 0.0f, 0.0f); 
    float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth); 
    targetPosition = new Vector3 (targetWidth, targetPosition.y, targetPosition.z); 
    r2D.MovePosition (targetPosition);  
} 
関連する問題