敌人脚本在我的塔防游戏C#上无法正常工作 [英] Enemy Script Isn't working properly on my Tower Defense game c#

查看:123
本文介绍了敌人脚本在我的塔防游戏C#上无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在关注youtube上"Brackeys"上的塔防游戏教程,我逐字跟进,但是有一个错误(在下面的照片上),它不会让我的敌人的预制件移动在现场.预制件都是产卵的,但是被卡在一个位置上. 任何帮助将不胜感激

I've recently been following a tutorial from "Brackeys" on youtube for a tower defence game, I followed it word for word but have an error (which is on the photos below) that won't let my enemy prefabs move in the scene. The prefabs are all spawning, but are stuck in the one spot. any help will be appreciated

谢谢米奇

using UnityEngine;

public class Enemy : MonoBehaviour 
{
    public float speed = 10f;
    private Transform target;
    private int wavePointIndex = 0;

    // Use this for initialization
    void Start() 
    {
        // An error pops up on the first frame for this line of code below
        target = Waypoints.points[0];
    }

    // Update is called once per frame
    void Update () 
    {
        // This is the main source of the error below.
        Vector3 dir = target.position - transform.position;
        transform.Translate (dir.normalized * speed * Time.deltaTime, Space.World);

        if (Vector3.Distance (transform.position, target.position) <= 0.4f) 
        {
            GetNextWayPoint ();
        }
    }

    void GetNextWayPoint()
    {
        if (wavePointIndex >= Waypoints.points.Length - 1) 
        {
            Destroy (gameObject);
            return;
        }
        wavePointIndex++;
        target = Waypoints.points[wavePointIndex];
    }
}

统一错误说明 上面有脚本的敌人预制件

推荐答案

您上传的图像显示您在编辑器中命名了Enemy脚本EnemyMover.这不是问题,但是您应该始终在场景中发布脚本名称,以免造成混淆.

The image you uploaded shows that you named Enemy script EnemyMover in your Editor. This is not a problem but you should always post script names as it is in your scene to avoid confusion.

根据您的说法,以下代码是问题所在:target = Waypoints.points[0];

According to you, this line of code is the problem: target = Waypoints.points[0];

问题是pointsTransform的数组,并且未初始化.

Issue is that points is an array of Transform and is not initialized.

存在三个可能的问题:

1 .您的Waypoint脚本未附加到 Waypoints GameObject. 选择 Waypoints 游戏对象,将Waypoint脚本拖入其中.必须将其附加到您的 Waypoints GameObject以便初始化 points数组变量.

1.Your Waypoint script is NOT attached to the Waypoints GameObject. Select the Waypoints GameObject, drag the Waypoint script into it. It must be attached to your Waypoints GameObject in order for it to initilize the points array variable.

2 .您的Waypoint脚本已附加到多个GameObjects.确保Waypoint脚本仅附加到一个GameObject,并且GameObject是所有航路点的父级.这个GameObject被命名为 Waypoints .

2.Your Waypoint script is attached to multiple GameObjects. Make sure that the Waypoint script is ONLY attached to one GameObject and that GameObject is the parent of all your waypoints. This GameObject is named Waypoints.

要对此进行验证,请从项目"选项卡中选择Waypoint脚本,右键单击该脚本,然后单击"在场景中查找引用" .它将向您显示Waypoint脚本所附加的每个GameObject.如果它连接到任何未命名为 Waypoints 的GameObject,请从该GameObject中删除脚本.

To verify this, select the Waypoint script from the Project Tab, right click on it and click Find Reference In Scene. It will show you every GameObject the Waypoint script is attached to. If it is attached to any GameObject that is not named Waypoints, remove the script from that GameObject.

3 .Waypoint脚本中的功能先于Enemy脚本中的功能被调用.有时在Unity中会发生这种情况.

3.Functions in your Waypoint script are being called before the ones in your Enemy script. This happens sometimes in Unity.

转到编辑-> 项目设置-> 脚本执行顺序.将Waypoint脚本拖到该顺序中,然后将Enemy脚本拖到oder中.点击应用".

Go to Edit->Project Settings->Script Execution Order. Drag the Waypoint script into the order then drag the Enemy script in the oder. Click Apply.

问题#3 的第二种解决方法是删除static关键字,并使用GameObject.Find("Waypoints");GetComponent<Waypoint>();.您可以在此处此处.请注意,如果您遵循删除static关键字的第二种解决方案,则可能很难理解本教程的其余部分.

Second solution to problem #3 is to remove the static keyword and use GameObject.Find("Waypoints"); and GetComponent<Waypoint>();. You can find complete scripts both scripts here and here. Note that if you follow this second solution of removing the static keyword, you may have hard time following the rest of the tutorial.

这篇关于敌人脚本在我的塔防游戏C#上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆