可能是hit.collider.gameObject.GetComponentInChildren的错误?

时间:2019-01-10 01:07:50

标签: c# unity3d

因此,总而言之,我正在尝试修改脚本,以便当我单击对象时,射线会击中该对象,在子级中查找一个组件并使之可用,以便以后可以引用它感。我可以进一步详细说明,所以不要害怕发表评论。

我尝试研究此问题,但在这种情况下,没有发现任何可以帮助我的东西。我也看过其他文章提到这可能是一个错误。我不确定这是否真的是错误,还是我做错了什么。

一切正常,除了:

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

Debug.Log("Other:" + other); <- This line of code gives me this in Unity:
  

其他:检测(战斗)
  UnityEngine.Debug:Log(Object)
  TankController:SetTargetPosition()(在
  资产/游戏/ Chisana /脚本/TankController.cs:63)
  TankController:Update()(位于Assets / Games / Chisana / Scripts / TankController.cs:27)

我在Unity中遇到的错误是:

  

NullReferenceException:对象引用未设置为对象的实例   TankController.SetTargetPosition()(在Assets / Games / Chisana / Scripts / TankController.cs:65处)   TankController.Update()(在Assets / Games / Chisana / Scripts / TankController.cs:27)

所有代码如下:

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;
}
}

此代码的结果应该是,当我单击一个对象时,它应该选择该对象,然后如果再次单击它,则应取消选择它。选择后,它应该转到我单击的位置,而取消选择后则不执行任何操作。

相反,当我单击一个对象时,错误会在控制台中弹出,并且该对象未被选中,因此不会移动。

1 个答案:

答案 0 :(得分:1)

为空的东西不是您要调试的东西。

Fighting self = GetComponent<Fighting>(); //this one is null
Fighting other = hit.collider.gameObject.GetComponentInChildren<Fighting>(); //this one isn't