c# - if 语句在 void Start(); 中单独工作;
问题描述
PawnHasFallen() 中的 if 语句仅在其在启动功能中有效并且他将此日志称为 Debug.Log($"{pawnName} has fall", this); 和Debug.Log("它不是工作伙伴"); 对于还没有落下的棋子。但是当它在 HitBowling 脚本中被调用时我不工作然后我只得到Debug.Log("It Aint working mate"); . 为什么当我在其他脚本中调用它时它不起作用?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pawn : MonoBehaviour
{
private GameObject pawnName;
public Transform tf;
public Rigidbody rb;
[SerializeField]
float eulerAngX;
[SerializeField]
float eulerAngY;
[SerializeField]
float eulerAngZ;
public void Start()
{
tf = GetComponent<Transform>();
rb = GetComponent<Rigidbody>();
PawnHasFallen();
}
public void PawnHasFallen()
{
eulerAngX = transform.localEulerAngles.x;
eulerAngY = transform.localEulerAngles.y;
eulerAngZ = transform.localEulerAngles.z;
Debug.Log("Calling this script");
if (!Mathf.Approximately(Vector3.Angle(Vector3.up, transform.up), 0f))
{
Debug.Log($"{pawnName} has fallen", this);
}
else
{
Debug.Log("It Aint working mate");
}
}
}
我调用另一个脚本的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HitBowling : MonoBehaviour
{
public Pawn pawn;
public GameObject pawnPrefab;
public void Start()
{
GameObject obj = pawnPrefab;
pawn = obj.GetComponent<Pawn>();
}
public IEnumerator OnTriggerEnter(Collider collision)
{
Debug.Log("Big balls");
//if the ball hits the end of the bowling ally start this
if (collision.gameObject.name == "Ball")
{
Debug.Log("Start de second wait");
//wait 5 second for starting the fuction
yield return new WaitForSeconds(5);
Debug.Log("Ended the waiting");
//this function sees if the pawn has fallen and then gives you points
pawn.PawnHasFallen();
}
}
}
解决方案
推荐阅读
- python - 科学蟒蛇的新手。我收到 AttributeError: 'ImmutableDensNDimArray' 错误,我不明白为什么
- xaml - xamarin 在 CollectionView 中形成更新项目
- c++ - 显示前 3 个项目
- node.js - 如何使用云函数中的 OrderByChild() 对 Datasnapshot 进行排序
- java - 如何让程序打印它的位置?
- android - 将自定义声音文件放在 Ionic 4 上的 OneSignal 推送通知的什么位置?
- java - 如何使用输入使我制作的简笔画移动?
- python - 限制发送的短信数量
- javascript - 在函数中使用 this 关键字设置属性
- sparql - 使用 SPARQL DBPEDIA 获取地名中的每个地名