C# hit.collider.gameObject.GetComponentChildren可能存在bug?

C# hit.collider.gameObject.GetComponentChildren可能存在bug?,c#,unity3d,C#,Unity3d,总之,我试图修改一个脚本,这样当我单击一个对象时,一条光线会击中该对象,在子对象中查找一个组件,并使其可用,以便在以后有意义时可以引用它。我可以进一步阐述,所以不要害怕发表评论 我试着研究这个问题,但在这种情况下,我找不到任何可以帮助我的东西。我还看到其他帖子提到这可能是一个bug。我不确定这是否真的是一个bug,或者我只是做错了什么 一切正常,除了: Fighting other = hit.collider.gameObject.GetComponentInChildren<Fight

总之,我试图修改一个脚本,这样当我单击一个对象时,一条光线会击中该对象,在子对象中查找一个组件,并使其可用,以便在以后有意义时可以引用它。我可以进一步阐述,所以不要害怕发表评论

我试着研究这个问题,但在这种情况下,我找不到任何可以帮助我的东西。我还看到其他帖子提到这可能是一个bug。我不确定这是否真的是一个bug,或者我只是做错了什么

一切正常,除了:

Fighting other = hit.collider.gameObject.GetComponentInChildren<Fighting>();

Debug.Log("Other:" + other); <- This line of code gives me this in Unity:
batting other=hit.collider.gameObject.getComponentChildren();
Debug.Log(“其他:+Other”);空值不是您要调试的内容。日志记录:
Fighting self=GetComponent()//这个是空的
Fighting other=hit.collider.gameObject.GetComponentChildren()//这个不是

我很确定unity没有bug:D您确定self或other
.Team
不是空的吗?谢谢您指出这一点。我的问题已经解决了。如果unity将这条线标记为问题,那会很有帮助。您的调试在第63行<代码>其他:检测(战斗)-UnityEngine.Debug:Log(Object)(位于TankController.cs:63)
错误出现在第65行<代码>NullReferenceException:(位于TankController.cs:65)。由于第65行是
if(self.Team==other.Team)
,根据第63行的信息,我们知道
other
不能为空。这里唯一能抛出NPE的是
self
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankController : MonoBehaviour
{

Vector3 targetPosition;
Vector3 lookAtTarget;
Quaternion playerRot;
float rotSpeed = 2;
float speed = 3;
bool moving = false;
public bool selected = false;

// Use this for initialization
//void Start()
//{

//}

// Update is called once per frame
void Update()
{
    if (Input.GetMouseButton(0))
    {
        SetTargetPosition();
    }
    if (moving)
        Move();
}

void SetTargetPosition()
{

    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 1000))
        {

            if (hit.collider.CompareTag("Hittable") == true && selected == true)
            {
                Debug.Log("Found Ground");
                targetPosition = hit.point;
                lookAtTarget = new Vector3(targetPosition.x - transform.position.x,
                transform.position.y,
                targetPosition.z - transform.position.z);
                playerRot = Quaternion.LookRotation(lookAtTarget);
                moving = true;
            }

            if (hit.collider.CompareTag("Unit") == true)
            {

                Debug.Log("Found Unit");

                Fighting self = GetComponent<Fighting>();

                Fighting other = hit.collider.gameObject.GetComponentInChildren<Fighting>();

                Debug.Log("Other:" + other);

                if (self.Team == other.Team)
                {
                    if (selected == false)
                    {
                        selected = true;
                        Debug.Log("Selected");
                    }
                    if (selected == true)
                    {
                        selected = false;
                        Debug.Log("Deselected");
                    }
                }
            }

        }
    }
}

void Move()
{
    transform.rotation = Quaternion.Slerp(transform.rotation,
                                            playerRot,
                                            rotSpeed * Time.deltaTime);
    transform.position = Vector3.MoveTowards(transform.position,
                                            targetPosition,
                                            speed * Time.deltaTime);

    if (transform.position == targetPosition)
        moving = false;
}
}
Fighting self = GetComponent<Fighting>(); //this one is null
Fighting other = hit.collider.gameObject.GetComponentInChildren<Fighting>(); //this one isn't