【发布时间】:2014-06-13 16:35:09
【问题描述】:
我正在制作一个游戏,玩家触摸屏幕并实例化一个对象。但是,我只希望在 Raycast 击中特定图层上的对象(或者如果更容易,则为特定标签)的对象时实例化该对象。我可以让光线投射出来并在我想要的地方实例化一个预制件,但是当我在检查层的部分中添加时,它会向我发送一个错误(NullReferenceException:Object reference not set to an instance of an object )。这似乎是一件很简单的事情,但我无法让它发挥作用。任何帮助将不胜感激!
var box : Transform;
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
if(roomController.noMore == false){
var hit : RaycastHit;
var mousePos : Vector3 = Input.mousePosition;
mousePos.z = 9;
var worldPos : Vector3 = camera.ScreenToWorldPoint(mousePos);
Debug.Log("Mouse pos: " + mousePos + " World Pos: " + worldPos + " Near Clip Plane: " + camera.nearClipPlane);
if(hit.collider.gameObject.layer == "Ground" && HierarchyType.collider != null){
clone = Instantiate(box, worldPos, Quaternion.identity);
noMore = true;
Destroy(this);
}
}
}
}
【问题讨论】:
标签: unity3d instantiation raycasting