我正在尝试在Unity中创建一个拼图游戏,通过游戏设计HQX'复制YouTube的教程。我写了教程第一部分的所有代码,Unity不会运行它,我不明白为什么。请帮忙..
using UnityEngine;
using System.Collections;
public class Movepiece : MonoBehaviour {
public string pieceStatus = "";
// Update is called once per frame
void Update()
{
if (pieceStatus != "locked")
{
Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPosition;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (other.gameObject.name == gameObject.name)
{
transform.position = other.gameObject.transform.position;
pieceStatus = "locked";
}
}
}
这是Unity给我的错误: Assets / Movepiece.cs(28,34):错误CS0103:名称`other'在当前上下文中不存在
答案 0 :(得分:2)
变化:
private void OnTriggerEnter2D(Collider2D collision)
为:
private void OnTriggerEnter2D(Collider2D other)
我建议您先看Unity official tutorials。他们对于初学者来说要好得多。
答案 1 :(得分:1)
private void OnTriggerEnter2D(Collider2D collision) {
if (other.gameObject.name == gameObject.name)
您将参数命名为collision
而不是other
。