简体   繁体   English

Unity C# 使用标签查找游戏对象

[英]Unity C# finding GameObjects using tags

I am just starting out with Unity and C# and i am trying to make an enemy chase after the player.我刚开始使用 Unity 和 C#,我正试图让敌人追逐玩家。 For the enemy to find and chase the player i am using the following code:为了让敌人找到并追逐玩家,我使用以下代码:

public GameObject attackingg;
public Entity attacking;
public int distance;

private bool canAttack;

void Start () {
    canAttack = true;
    if(attackingg = null){
        attackingg = GameObject.FindGameObjectWithTag("Player");
        attacking = attackingg.GetComponent<Entity>();
    }
}

void Update () {

    if(attacking.rigidbody2D.transform.position.y>rigidbody2D.transform.position.y - distance)
    {
        rigidbody2D.transform.position += Vector3.up * speed * Time.deltaTime;
    }
    if(attacking.rigidbody2D.transform.position.y<rigidbody2D.transform.position.y - distance)
    {
        rigidbody2D.transform.position += Vector3.down * speed * Time.deltaTime;
    }
    if(attacking.rigidbody2D.transform.position.x>rigidbody2D.transform.position.x+ distance)
    {
        rigidbody2D.transform.position += Vector3.right * speed * Time.deltaTime;
    }
    if(attacking.rigidbody2D.transform.position.x<rigidbody2D.transform.position.x - distance)
    {
        rigidbody2D.transform.position += Vector3.left * speed * Time.deltaTime;
    }
    if (Vector2.Distance (rigidbody2D.transform.position, attacking.transform.position) <= distance && canAttack) {
        attackEntity();
        StartCoroutine(waitForAttack());

    }
}

When running the game, i get the following error:运行游戏时,出现以下错误:

NullReferenceException: Object reference not set to an instance of an object AttackingMob.Update () (at Assets/Code/Entities/Mobs/AttackingMob.cs:22) NullReferenceException:Object 引用未设置为 object AttackingMob.Update () 的实例(位于 Assets/Code/Entities/Mobs/AttackingMob.cs:22)

Why is this happening and what can i do to fix my problem?为什么会发生这种情况,我该怎么做才能解决我的问题? I really appreciate any help, thanks.我真的很感激任何帮助,谢谢。

You need to tag the player with the "Player" tag.您需要使用“玩家”标签来标记玩家。 You have to do this through the editor window in Unity.您必须通过 Unity 中的编辑器窗口执行此操作。

The other problem you have is that you are trying to assign null instead of actually checking for null.您遇到的另一个问题是您试图分配空值而不是实际检查空值。

if(attackingg = null)
{}

should be:应该:

if(attackingg == null)
{}

You need to add the " Player tag to the Player GameObject in the Inspector.您需要将 " Player标签添加到检查器中的 Player GameObject。

And the error:和错误:

Object reference not set to an instance of an object你调用的对象是空的

Just check that you have assigned the respective GameObject in the Inspector.只需检查您是否已在 Inspector 中分​​配了相应的 GameObject。 And also, in the line if (attacking = null) {} You are supposed to check that the GameObject is NULL by replacing "=" with "=="而且,在if (attacking = null) {}您应该通过将"=" with "=="替换"=" with "=="来检查 GameObject 是否为 NULL

You are assigning null instead of checking null.您正在分配 null 而不是检查 null。

Your code你的代码

if (attackingg = null)

should be应该

if (attackingg == null)

In order not to cause this NullPointerException, just assign variable in inspector if the gameobject already spawned in the scene because you already have global variable for that.为了不导致此 NullPointerException,如果游戏对象已经在场景中生成,则只需在检查器中分配变量,因为您已经为此设置了全局变量。

public GameObject attackingg = null; // assign gameobject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM