[英]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.