c# - unity If & else 语句
问题描述
所以我有 5 个不同的对象,每次单击特定对象时,它们都应该按顺序放置。它似乎没有按顺序排列,而是跳过了第一个并进入了第二个。所以不是去check1,而是直接去check2。
这是我的代码:
if (IsBehindHeld == true)
{
Vector3 MousePos;
MousePos = Input.mousePosition;
MousePos = Camera.main.ScreenToWorldPoint(MousePos);
if (Check1.transform.childCount != 1)
{
this.gameObject.transform.position = Check1.transform.position;
_rb2.isKinematic = true;
locked = true;
this.transform.parent = Check1.transform;
}
else
{
if (Check2.transform.childCount != 1 )
{
this.gameObject.transform.position = Check2.transform.position;
_rb2.isKinematic = true;
locked = true;
this.transform.parent = Check2.transform;
}
else
{
if (Check3.transform.childCount != 1)
{
this.gameObject.transform.position = Check3.transform.position;
_rb2.isKinematic = true;
locked = true;
this.transform.parent = Check3.transform;
}
else
{
}
}
}
}
解决方案
请发布有关检查碰撞器、对象层次结构和组件的更多信息,每个都必须了解更多。此外,如果您尝试使用 else if,它会更容易阅读:
if (IsBehindHeld == true)
{
Vector3 MousePos;
MousePos = Input.mousePosition;
MousePos = Camera.main.ScreenToWorldPoint(MousePos);
if (Check1.transform.childCount != 1)
{
this.gameObject.transform.position = Check1.transform.position;
_rb2.isKinematic = true;
locked = true;
this.transform.parent = Check1.transform;
}
else if (Check2.transform.childCount != 1)
{
this.gameObject.transform.position = Check2.transform.position;
_rb2.isKinematic = true;
locked = true;
this.transform.parent = Check2.transform;
}
else if (Check3.transform.childCount != 1)
{
this.gameObject.transform.position = Check3.transform.position;
_rb2.isKinematic = true;
locked = true;
this.transform.parent = Check3.transform;
}
}
在 if 语句之后可以使用多个 else if 语句。它只会在 if 条件评估为 false 时执行。因此,可以执行 if 或 else if 语句之一,但不能同时执行。让我知道事情的后续!
推荐阅读
- javascript - 尝试使用 axios 进行 VueJS API 调用以在名为 prijzen 的组件中获取 json
- html - 函数值如何显示在 HTML 标记中,并带有 addEventListerner click 的返回值?
- ssl - 使用 pip 安装证书验证失败,主要问题,(_ssl.c:1108)
- javascript - useMemo on .map 语句中使用的函数
- python - 对 DASK 中的列中的每个值运行 mysql 查询
- python - 我希望我的角色只要按住按钮就可以做某事,然后在松开按钮后恢复正常
- reactjs - 具有给定令牌的 Axios 拦截器,React
- javascript - vanilla javascript使用链接的点击但不同的命令
- javascript - 触发日期更改时未触发 ng-change 事件
- java - 如何在 Spring Boot 中捕获客户端参数错误的“请求处理失败”?