2016-12-13 171 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?

和:

'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);  
    } 
} 

請幫幫忙!非常感謝!

+0

100%確保您的項目將與新舊版本統一的工作,我會建議建立一些地方組件的成員像'Rigidbody2D meRigidbody = GetComponent ()'這將確保你使用正確的'Component'類型。 –

+0

你在哪裏得到'rigidbody2D'變量,它是哪種類型? –

回答

3

您不能再訪問連接到GameObject direc的組件像你過去一樣。您必須現在使用GetComponent。您的代碼是使用Unity 4及以下有效的,但不能與5

這些都是錯誤的:

rigidbody2D.MovePosition(targetPosition); 

float SantaBagWidth = renderer.bounds.extents.x; 

要修復它,用Rigidbody2D rigidbody2D;聲明rigidbody2D

然後使用GetComponent與得到渲染GetComponent<Renderer>().bounds.extents.x;

整個代碼:

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將會看到您的評論。我也相信這會發生** only only **當您在5.x版本中導入舊項目時,然後在Unity詢問您是否應該使用舊的API或類似的東西時點擊yes。 – Programmer

+0

非常感謝你們! –

1

使用 「MonoBehaviour屬性」(如變換,渲染,...是貶值了。

相反,使用顯式功能GetComponent

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

您剛體相同的通知。它緩存在喚醒功能並在固定更新中使用它

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);  
}