简体   繁体   English

每次我尝试测试运行时都会遇到错误当前上下文中不存在名称“播放器”。 我找不到解决办法

[英]every time i try to test run i run in to an error The name 'Player' does not exist in the current context. i cant find a solution

I am making a game and in the game im trying to make the enemy go towards the player when the player steps into the sight of the enemy.我正在制作一个游戏,在游戏中,当玩家进入敌人的视线时,我试图让敌人走向玩家。 I cant figure out how to make the name (player) mean anything im very new at this its my second attempt on makin a game please help me im very confused im running c#我不知道如何让这个名字(玩家)意味着任何我很新的东西这是我第二次尝试制作游戏请帮助我我很困惑我正在运行 c#

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Enemy : MonoBehaviour
{
  private Transform _target;

  [SerializeField] private float _speed = 9;
    
   void Awake() {
       _target = FindObjectOfType(Player)().transform;
   }

   void update() {

       transform.position = Vector3.MoveTowards(transform.position,_target.position, _speed * Time.deltaTime);
    }

}

You need to use the typeof operator instead of passing the class name.您需要使用typeof运算符而不是传递类名。

void Awake() {
    _target = FindObjectOfType(typeof(Player))().transform;
}

暂无
暂无

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

相关问题 名称“…”在当前上下文中不存在。 错误 - The name “…” does not exist in the current context. error 当前上下文中不存在“会话”。 我该如何使用它? - 'Session' does not exist in the current context. How can I use it? C#剃刀错误:在当前上下文中名称“ i”不存在 - c# razor error: The name 'i' does not exist in this current context “当前上下文中不存在名称‘DisplayAlert’。” Xamarin C# - “The name ”DisplayAlert“ does not exist in current context.” Xamarin C# 当前上下文中不存在名称 userInput。 为什么? - The name userInput does not exist in the current context. Why? 该名称在当前上下文中不存在。 不上课 - The name does not exist in the current context. Not seeing class 为什么我在这个 try-catch 块中收到“错误:当前上下文中不存在数据表”? - Why am I getting "Error: datatable does not exist in current context" in this try-catch block? 如何解决“当前上下文中不存在名称'页面'” - How do I fix “The name 'page' does not exist in the current context” 如何修复“当前上下文中不存在名称‘脚本’”? - how do I fix "The name 'script' does not exist in the current context"? C#error1:当前上下文中不存在名称“DateTimeStyles”。 error2:找不到类型或命名空间名称“CultureInfo” - C# error1:The name 'DateTimeStyles' does not exist in the current context. error2:The type or namespace name 'CultureInfo' could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM