c# - UnityEngine.Component 不包含定义...C#

标签 c# unity3d

我在家里和大学里的机器安装了不同版本的 Unity。在我家较新,在大学较年长,我想知道这是否是导致我出现问题的原因。游戏运行良好,直到我尝试在家用计算机上进一步开发它。

得到两条错误信息:

“UnityEngine.Component”不包含“bounds”的定义,并且找不到类型为“UnityEngine.Component”的扩展方法“bounds”。您是否缺少程序集引用?

和:

“UnityEngine.Component”不包含“MovePosition”的定义,并且找不到“UnityEngine.Component”类型的扩展方法“MovePosition”。您是否缺少程序集引用?

这是我的代码:

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

求助!非常感谢!

最佳答案

您不能再像过去那样直接访问附加到游戏对象的组件。您现在必须使用GetComponent .您的代码对 Unity 4 及更低版本有效,但对 5 无效。

这些是错误:

rigidbody2D.MovePosition(targetPosition);

float SantaBagWidth = renderer.bounds.extents.x;

要修复它,请声明 rigidbody2DRigidbody2D 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);
}

关于c# - UnityEngine.Component 不包含定义...C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41123057/

相关文章:

unity3d - Unity Json.net System.Reflection.Emit 在 iOS 中的错误

android - 通过表面着色器 (Unity) 用其他纹理交换 MainTex 像素

C# 自定义 Invoke 方法调用各种其他方法

c# - 如何从 Controller 将数据传递到 View 中的jquery

c# - 将项目移动到 Entity Framework 迁移中的项目列表

c# - 从文本文件中读取并更新

c# - 使用 jquery 设置一个值,在提交数据时删除对 ViewModel 的绑定(bind)

c# - 纹理在编辑器中加载但不独立加载(显示为粉红色)

audio - Pre Spatializer 效果 - Unity 和 Google VR 音频

c# - 如何在jquery中独立于浏览器捕获精确的td值