简体   繁体   English

为什么我不能将我的播放器附加到脚本中的转换?

[英]Why can't I attach my Player to a transform in my script?

I am trying to make a script where the zombie looks for the player and if the player enters the boundaries it will start chasing it.我正在尝试制作一个脚本,僵尸在其中寻找玩家,如果玩家进入边界,它将开始追逐它。 My problem is that I can't attach my player to the script in the inspector and I am unsure why.我的问题是我无法将我的播放器附加到检查器中的脚本,我不确定为什么。 I don't have a problem with the code(at least I don't think so) only with it not attaching.我对代码没有问题(至少我不这么认为),只是它没有附加。 The player is a prefab from the unity Standard assets.播放器是统一标准资产的预制件。

Edit: The script is attached to a prefab in my assets.编辑:脚本附加到我资产中的预制件上。 The player in in the hierarchy.层次结构中的玩家。

Just in case here is my code以防万一这是我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ZK_attack : MonoBehaviour
{    
    public Transform Player;
    public float MoveSpeed = 4.5f;
    public float MaxDist = 4.0f;
    public float MinDist = 1.5f;
    private Coroutine animat = null;
    private Animator anim;


    void Update()
    {
        

        transform.LookAt(Player);

        if (Vector3.Distance(transform.position, Player.position) >= MinDist)
        {
            transform.position += transform.forward * MoveSpeed * Time.deltaTime;
            anim.SetBool("inRadius", true);

            if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
            {
                anim.SetBool("AttackingPlayer", true);


            }

        }

        if (Vector3.Distance(transform.position, Player.position) <= MinDist)
        {
            anim.SetBool("inRadius", false);
        }

        if (Vector3.Distance(transform.position, Player.position) >= MaxDist)
        {
            anim.SetBool("AttackingPlayer", false);
        }
    }
}

Ok, the problem was that you can only drag objects into scripts that are in your scene already, unless they are prefabs themselves in the assests folder, even though it is on an object.好的,问题是您只能将对象拖到已经在场景中的脚本中,除非它们本身是 assests 文件夹中的预制件,即使它位于 object 上。 To accomplish what you need, in your Start() add为了完成你需要的,在你的 Start() 添加

Player = GameObject.Find("...name of object you wish to set as Player").transform;

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

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