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'对象'不包含' transform'的定义没有扩展方法'转换'接受类型'对象'的第一个参数。可以找到(你是否缺少using指令或程序集引用?)MakeTwoPoints3D.cs 23 Active
答案 0 :(得分:2)
Object
是GameObject
和GameObject
的父类,是Transform
类型的成员。如果您尝试从transform
类的实例访问Object
,则会显示以下错误:
Object'不包含'transform'的定义
如此精确的实例化和将结果对象用作GameObject的方式正如Quantic在comment中所说:
GameObject cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity) as GameObject;
OR
GameObject cylinder = (GameObject) Instantiate(cylinderPrefab, position, Quaternion.identity);
在GameObject之外的其他类型的情况下,在使用组件之前始终使用null-check来确保安全性。例如:
Rigidbody rb = Instantiate(somePrefab) as Rigidbody;
if(rb != null)
// use it here
希望这会有所帮助。