Object “类型”的对象;“游戏对象”;已经被摧毁了

Object “类型”的对象;“游戏对象”;已经被摧毁了,object,user-interface,Object,User Interface,目前正在尝试制作一款自上而下的“隐形”游戏。不幸的是,当移动到下一个级别时,当我被敌人抓住时,UI不希望弹出。通常情况下,我会在屏幕上看到文字“你被抓到了”,但当通过触发器切换级别时,似乎不允许UI弹出 用户界面代码 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class GameUI : Mono

目前正在尝试制作一款自上而下的“隐形”游戏。不幸的是,当移动到下一个级别时,当我被敌人抓住时,UI不希望弹出。通常情况下,我会在屏幕上看到文字“你被抓到了”,但当通过触发器切换级别时,似乎不允许UI弹出

用户界面代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameUI : MonoBehaviour
{
  public GameObject gameLoseUI;
  public GameObject gameWinUI;
  bool gameIsOver;
  void Start()
  {
    Guard.OnGuardHasSpottedPlayer += ShowGameLoseUI;
    FindObjectOfType<Player>().OnReachedEndOfLevel += ShowGameWinUI;
  }

  void Update()
  {
    if (gameIsOver){
        if (Input.GetKeyDown(KeyCode.Space)){
            SceneManager.LoadScene(0);
        }
        
        
    }
}

void ShowGameWinUI(){
    OnGameOver(gameWinUI);
}

void ShowGameLoseUI(){
    OnGameOver(gameLoseUI);
}

public void OnGameOver (GameObject gameOverUI)
{
    gameOverUI.SetActive(true);
    gameIsOver = true;
    Guard.OnGuardHasSpottedPlayer -= ShowGameLoseUI;
    FindObjectOfType<Player>().OnReachedEndOfLevel -= ShowGameWinUI;
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Guard : MonoBehaviour
{
  public static event System.Action OnGuardHasSpottedPlayer;

  public float speed = 5;
  public float waitTime = .3f;
  public float turnSpeed = 90;
  public float TimeToSpotPlayer = .5f;

  public Light spotlight;
  public float ViewDistance;
  public LayerMask viewMask;

  float ViewAngle;
  float playerVisibleTimer;

  public Transform PathHolder;
  Transform player;
  Color OriginalSpotlightColour;
  void Start()
  {
    player = GameObject.FindGameObjectWithTag("Player").transform;
    ViewAngle = spotlight.spotAngle;
    OriginalSpotlightColour = spotlight.color;

    Vector3[] waypoints = new Vector3[PathHolder.childCount];
    for (int i = 0; i < waypoints.Length; i++) {
        waypoints[i] = PathHolder.GetChild(i).position;
        waypoints[i] = new Vector3(waypoints[i].x, transform.position.y, waypoints[i].z);
    }
    StartCoroutine(Followpath(waypoints));
}

 void Update()
  {
     if (CanSeePlayer()){
        playerVisibleTimer += Time.deltaTime;
     }else{
        playerVisibleTimer -= Time.deltaTime;
     }
     playerVisibleTimer = Mathf.Clamp(playerVisibleTimer, 0, TimeToSpotPlayer);
     spotlight.color = Color.Lerp(OriginalSpotlightColour, Color.red, playerVisibleTimer / TimeToSpotPlayer);

     if (playerVisibleTimer >= TimeToSpotPlayer)
     {
        if (OnGuardHasSpottedPlayer != null)
        {
            OnGuardHasSpottedPlayer();
        }
    }
}

bool CanSeePlayer()
{
    if (Vector3.Distance(transform.position,player.position) < ViewDistance)
    {
        Vector3 dirToPlayer = (player.position - transform.position).normalized;
        float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer);
        if (angleBetweenGuardAndPlayer < ViewAngle / 2f)
        {
          if (!Physics.Linecast (transform.position, player.position, viewMask)) {
                return true;
            }
        }
    }
    return false;
}
 IEnumerator Followpath(Vector3[] waypoint) {
    transform.position = waypoint[0];

    int targetWaypointIndex = 1;
    Vector3 targetWaypoint = waypoint [targetWaypointIndex];
    transform.LookAt (targetWaypoint);

    while (true)
    {
        transform.position = Vector3.MoveTowards (transform.position, targetWaypoint, speed * Time.deltaTime);
        if (transform.position == targetWaypoint)
        {
            targetWaypointIndex = (targetWaypointIndex + 1) % waypoint.Length;
            targetWaypoint = waypoint [targetWaypointIndex];
            yield return new WaitForSeconds (waitTime);
            yield return StartCoroutine (TurnToFace (targetWaypoint));
        }
        yield return null;
    }
}

IEnumerator TurnToFace(Vector3 lookTarget)
{
    Vector3 dirToLookTarget = (lookTarget-transform.position).normalized;
    float targetAngle = 90- Mathf.Atan2 (dirToLookTarget.z, dirToLookTarget.x) * Mathf.Rad2Deg;

    while (Mathf.Abs(Mathf.DeltaAngle(transform.eulerAngles.y, targetAngle)) > 0.05f)
        {
        float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetAngle, turnSpeed * Time.deltaTime);
        transform.eulerAngles = Vector3.up * angle;
        yield return null;
    }      
}
void OnDrawGizmos()
{
    Vector3 startPosition = PathHolder.GetChild(0).position;
    Vector3 previousPosition = startPosition;

    foreach (Transform Waypoint in PathHolder)
    {
        Gizmos.DrawSphere(Waypoint.position, .3f);
        Gizmos.DrawLine(previousPosition, Waypoint.position);
        previousPosition = Waypoint.position;
    }
    Gizmos.DrawLine(previousPosition, startPosition);

    Gizmos.color = Color.red;
    Gizmos.DrawRay(transform.position, transform.forward * ViewDistance);
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MoveScene : MonoBehaviour
{
   [SerializeField] private string loadLevel;
     void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            SceneManager.LoadScene(loadLevel);
        }
    }
 }
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;

public class Player : MonoBehaviour{

    public event System.Action OnReachedEndOfLevel;

    public float moveSpeed = 7;
    public float SmoothMoveTime = .1f;
    public float TurnSpeed = 8;

    float angle;
    float SmoothInputMagnitude;
    float SmoothMoveVelocity;
    Vector3 velocity;

   new Rigidbody rigidbody;
    bool disabled;

    void Start()
    {
      rigidbody = GetComponent<Rigidbody>();
        Guard.OnGuardHasSpottedPlayer += Disable;
    }

    void Update()
    {
        Vector3 inputDirection = Vector3.zero;
        if (!disabled)
        {
            inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 
Input.GetAxisRaw("Vertical")).normalized;
        }
        float inputMagnitude = inputDirection.magnitude;
        SmoothInputMagnitude = Mathf.SmoothDamp(SmoothInputMagnitude, 
inputMagnitude, ref SmoothMoveVelocity, SmoothMoveTime);

        float targetAngle = Mathf.Atan2 (inputDirection.x, inputDirection.z) * Mathf.Rad2Deg;
        angle = Mathf.LerpAngle(angle, targetAngle, Time.deltaTime * TurnSpeed * inputMagnitude);

        velocity = transform.forward * moveSpeed * SmoothInputMagnitude;
    }

        void OnTriggerEnter(Collider hitcollider){
        if (hitcollider.tag =="Finish")
        {
            Disable();
            if (OnReachedEndOfLevel != null)
            {
                OnReachedEndOfLevel();
            }
        }
    }

    void Disable()
    {
        disabled = true;
    }
   void FixedUpdate()
    {
        rigidbody.MoveRotation(Quaternion.Euler(Vector3.up * angle));
        rigidbody.MovePosition(rigidbody.position + velocity * Time.deltaTime);
    }

    private void OnDestroy()
    {
        Guard.OnGuardHasSpottedPlayer -= Disable;
    }
}
玩家代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameUI : MonoBehaviour
{
  public GameObject gameLoseUI;
  public GameObject gameWinUI;
  bool gameIsOver;
  void Start()
  {
    Guard.OnGuardHasSpottedPlayer += ShowGameLoseUI;
    FindObjectOfType<Player>().OnReachedEndOfLevel += ShowGameWinUI;
  }

  void Update()
  {
    if (gameIsOver){
        if (Input.GetKeyDown(KeyCode.Space)){
            SceneManager.LoadScene(0);
        }
        
        
    }
}

void ShowGameWinUI(){
    OnGameOver(gameWinUI);
}

void ShowGameLoseUI(){
    OnGameOver(gameLoseUI);
}

public void OnGameOver (GameObject gameOverUI)
{
    gameOverUI.SetActive(true);
    gameIsOver = true;
    Guard.OnGuardHasSpottedPlayer -= ShowGameLoseUI;
    FindObjectOfType<Player>().OnReachedEndOfLevel -= ShowGameWinUI;
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Guard : MonoBehaviour
{
  public static event System.Action OnGuardHasSpottedPlayer;

  public float speed = 5;
  public float waitTime = .3f;
  public float turnSpeed = 90;
  public float TimeToSpotPlayer = .5f;

  public Light spotlight;
  public float ViewDistance;
  public LayerMask viewMask;

  float ViewAngle;
  float playerVisibleTimer;

  public Transform PathHolder;
  Transform player;
  Color OriginalSpotlightColour;
  void Start()
  {
    player = GameObject.FindGameObjectWithTag("Player").transform;
    ViewAngle = spotlight.spotAngle;
    OriginalSpotlightColour = spotlight.color;

    Vector3[] waypoints = new Vector3[PathHolder.childCount];
    for (int i = 0; i < waypoints.Length; i++) {
        waypoints[i] = PathHolder.GetChild(i).position;
        waypoints[i] = new Vector3(waypoints[i].x, transform.position.y, waypoints[i].z);
    }
    StartCoroutine(Followpath(waypoints));
}

 void Update()
  {
     if (CanSeePlayer()){
        playerVisibleTimer += Time.deltaTime;
     }else{
        playerVisibleTimer -= Time.deltaTime;
     }
     playerVisibleTimer = Mathf.Clamp(playerVisibleTimer, 0, TimeToSpotPlayer);
     spotlight.color = Color.Lerp(OriginalSpotlightColour, Color.red, playerVisibleTimer / TimeToSpotPlayer);

     if (playerVisibleTimer >= TimeToSpotPlayer)
     {
        if (OnGuardHasSpottedPlayer != null)
        {
            OnGuardHasSpottedPlayer();
        }
    }
}

bool CanSeePlayer()
{
    if (Vector3.Distance(transform.position,player.position) < ViewDistance)
    {
        Vector3 dirToPlayer = (player.position - transform.position).normalized;
        float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer);
        if (angleBetweenGuardAndPlayer < ViewAngle / 2f)
        {
          if (!Physics.Linecast (transform.position, player.position, viewMask)) {
                return true;
            }
        }
    }
    return false;
}
 IEnumerator Followpath(Vector3[] waypoint) {
    transform.position = waypoint[0];

    int targetWaypointIndex = 1;
    Vector3 targetWaypoint = waypoint [targetWaypointIndex];
    transform.LookAt (targetWaypoint);

    while (true)
    {
        transform.position = Vector3.MoveTowards (transform.position, targetWaypoint, speed * Time.deltaTime);
        if (transform.position == targetWaypoint)
        {
            targetWaypointIndex = (targetWaypointIndex + 1) % waypoint.Length;
            targetWaypoint = waypoint [targetWaypointIndex];
            yield return new WaitForSeconds (waitTime);
            yield return StartCoroutine (TurnToFace (targetWaypoint));
        }
        yield return null;
    }
}

IEnumerator TurnToFace(Vector3 lookTarget)
{
    Vector3 dirToLookTarget = (lookTarget-transform.position).normalized;
    float targetAngle = 90- Mathf.Atan2 (dirToLookTarget.z, dirToLookTarget.x) * Mathf.Rad2Deg;

    while (Mathf.Abs(Mathf.DeltaAngle(transform.eulerAngles.y, targetAngle)) > 0.05f)
        {
        float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetAngle, turnSpeed * Time.deltaTime);
        transform.eulerAngles = Vector3.up * angle;
        yield return null;
    }      
}
void OnDrawGizmos()
{
    Vector3 startPosition = PathHolder.GetChild(0).position;
    Vector3 previousPosition = startPosition;

    foreach (Transform Waypoint in PathHolder)
    {
        Gizmos.DrawSphere(Waypoint.position, .3f);
        Gizmos.DrawLine(previousPosition, Waypoint.position);
        previousPosition = Waypoint.position;
    }
    Gizmos.DrawLine(previousPosition, startPosition);

    Gizmos.color = Color.red;
    Gizmos.DrawRay(transform.position, transform.forward * ViewDistance);
  }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MoveScene : MonoBehaviour
{
   [SerializeField] private string loadLevel;
     void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            SceneManager.LoadScene(loadLevel);
        }
    }
 }
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;

public class Player : MonoBehaviour{

    public event System.Action OnReachedEndOfLevel;

    public float moveSpeed = 7;
    public float SmoothMoveTime = .1f;
    public float TurnSpeed = 8;

    float angle;
    float SmoothInputMagnitude;
    float SmoothMoveVelocity;
    Vector3 velocity;

   new Rigidbody rigidbody;
    bool disabled;

    void Start()
    {
      rigidbody = GetComponent<Rigidbody>();
        Guard.OnGuardHasSpottedPlayer += Disable;
    }

    void Update()
    {
        Vector3 inputDirection = Vector3.zero;
        if (!disabled)
        {
            inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 
Input.GetAxisRaw("Vertical")).normalized;
        }
        float inputMagnitude = inputDirection.magnitude;
        SmoothInputMagnitude = Mathf.SmoothDamp(SmoothInputMagnitude, 
inputMagnitude, ref SmoothMoveVelocity, SmoothMoveTime);

        float targetAngle = Mathf.Atan2 (inputDirection.x, inputDirection.z) * Mathf.Rad2Deg;
        angle = Mathf.LerpAngle(angle, targetAngle, Time.deltaTime * TurnSpeed * inputMagnitude);

        velocity = transform.forward * moveSpeed * SmoothInputMagnitude;
    }

        void OnTriggerEnter(Collider hitcollider){
        if (hitcollider.tag =="Finish")
        {
            Disable();
            if (OnReachedEndOfLevel != null)
            {
                OnReachedEndOfLevel();
            }
        }
    }

    void Disable()
    {
        disabled = true;
    }
   void FixedUpdate()
    {
        rigidbody.MoveRotation(Quaternion.Euler(Vector3.up * angle));
        rigidbody.MovePosition(rigidbody.position + velocity * Time.deltaTime);
    }

    private void OnDestroy()
    {
        Guard.OnGuardHasSpottedPlayer -= Disable;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用System.Security.Cryptography.X509证书;
使用UnityEngine;
公共类玩家:单一行为{
公共事件系统。达到内禀水平的行动;
公共浮动速度=7;
公共浮动时间=.1f;
公共浮动转速=8;
浮动角;
浮点平滑输入量;
浮动速度;
矢量3速度;
新刚体;
残疾儿童;
void Start()
{
刚体=GetComponent();
Guard.OnGuardHasSpottedPlayer+=禁用;
}
无效更新()
{
Vector3输入方向=Vector3.0;
如果(!已禁用)
{
inputDirection=新矢量3(Input.GetAxisRaw(“水平”),0,
GetAxisRaw(“垂直”)。标准化;
}
浮点输入幅值=输入方向.magnity;
SmoothInputMagnitude=数学平滑阻尼(SmoothInputMagnitude,
输入幅值、参考平滑移动速度、平滑移动时间);
float targetAngle=Mathf.Atan2(inputDirection.x,inputDirection.z)*Mathf.Rad2Deg;
角度=Mathf.LerpAngle(角度、目标角度、时间增量*转速*输入幅值);
速度=变换。前进*移动速度*平滑输入幅度;
}
无效对撞机(对撞机对撞机){
if(hitcliller.tag==“Finish”)
{
禁用();
如果(OnReacheEndoflevel!=null)
{
OnReachedEndOfLevel();
}
}
}
无效禁用()
{
禁用=真;
}
void FixedUpdate()
{
刚体.MoveRotation(四元数.Euler(Vector3.up*角度));
刚体.MovePosition(刚体.position+速度*时间.deltaTime);
}
私有void OnDestroy()
{
Guard.OnGuardHasSpottedPlayer-=禁用;
}
}

您好,欢迎来到SO。我建议在您的问题中添加更多的细节和可能更多的代码示例,以获得更多的吸引力。没有人能在不了解您的代码库的情况下回答这些问题。@johannburges这就是所有代码,请欣赏。