我在家裏和大學的機器上安裝了不同版本的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);
}
}
請幫幫忙!非常感謝!
100%確保您的項目將與新舊版本統一的工作,我會建議建立一些地方組件的成員像'Rigidbody2D meRigidbody = GetComponent()'這將確保你使用正確的'Component'類型。 –
你在哪裏得到'rigidbody2D'變量,它是哪種類型? –