[英]How can I fix the errors transform not exist?
using UnityEngine;
using System.Collections;
public class MakeTwoPoints3D : MonoBehaviour {
public GameObject cylinderPrefab;
// Use this for initialization
void Start () {
CreateCylinderBetweenPoints(Vector3.zero, new Vector3(10, 10, 10), 0.5f);
}
void CreateCylinderBetweenPoints(Vector3 start, Vector3 end, float width)
{
var offset = end - start;
var scale = new Vector3(width, offset.magnitude / 2.0f, width);
var position = start + (offset / 2.0f);
Object cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity);
cylinder.transform.up = offset;
cylinder.transform.localScale = scale;
}
// Update is called once per frame
void Update () {
}
}
在兩行上出現相同的錯誤:
cylinder.transform.up = offset;
cylinder.transform.localScale = scale;
嚴重性代碼說明項目文件行抑制狀態錯誤CS1061“對象”不包含“變換”的定義,並且找不到找到接受類型為“對象”的第一個參數的擴展方法“變換”(您是否缺少using指令或程序集引用?)MakeTwoPoints3D.cs 23有效
Object
是GameObject
父類,並且GameObject
是Transform
類型的成員。 如果嘗試從Object
類的實例訪問transform
,它將顯示以下錯誤:
對象”不包含“變換”的定義
如此精確的實例化和將結果對象用作GameObject的方法,正如Quantic在評論中所說:
GameObject cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity) as GameObject;
要么
GameObject cylinder = (GameObject) Instantiate(cylinderPrefab, position, Quaternion.identity);
對於非GameObject的其他類型,在使用組件之前始終應使用null檢查以確保安全。 例如:
Rigidbody rb = Instantiate(somePrefab) as Rigidbody;
if(rb != null)
// use it here
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.