首页 > 解决方案 > 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();

        }

    }

}

标签: c#unity3d

解决方案


推荐阅读