2016-12-13 125 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);  
}