脚本导致 Unity 无响应并冻结

问题描述 投票:0回答:0

我写了一个脚本。该脚本的目的是停用称为“目标”的对象,随机选择一个目标并激活它。

我使用目标让 NavMeshAgent 前往它们。当 NavMeshAgent 到达之前激活的那个时,它会被停用,并随机激活另一个。

问题是,当我制作脚本并去测试它时,在播放模式下,Unity 冻结了,并且查看任务管理器,我发现它停止响应。我不得不从任务管理器中关闭 Unity。当我在检查器中停用脚本时,当我在游戏模式下测试它时,我的游戏运行得很好。

我想先补充一件事。在 Unity 开始冻结之前,我收到此错误:“LS 分配器 ALLOC_TEMP_TLS,底层分配器 ALLOC_TEMP_MAIN 具有未释放的分配,大小 560”,但当 Unity 开始冻结时,我不再收到它。

我把代码留在这里,看看是否有人能找到问题:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class TargetManager : MonoBehaviour
{
    public Transform[] targets;

    private void Start()
    {
        // Obtener todos los targets del objeto
        targets = GetComponentsInChildren<Transform>();
        // Eliminar el primer elemento del array, ya que es el propio objeto y no queremos que sea considerado como un target
        targets = targets.Skip(1).ToArray();

        // Desactivar todos los targets al inicio
        foreach (Transform target in targets)
        {
            target.gameObject.SetActive(false);
        }

        // Activar un target aleatorio al inicio
        ActivateRandomTarget();
    }

    public void ActivateRandomTarget()
    {
        // Desactivar todos los targets
        foreach (Transform target in targets)
        {
            target.gameObject.SetActive(false);
        }

        // Obtener un índice aleatorio del array
        int randomIndex = Random.Range(0, targets.Length);
        // Activar el target aleatorio
        targets[randomIndex].gameObject.SetActive(true);
    }
}

我找不到代码的哪一部分可能导致此问题,但我认为存在一些循环或其他东西,因为游戏只会在几秒钟过去后才会冻结。抱歉,我没有太多编程经验。

我尝试优化代码,但它一直给我一个错误。我在网上没找到任何东西,不知道是我没搜好还是真的什么都没有。

我尝试在 Unity 冻结时查看 Profiler,但无法告诉您问题出在哪里。

这里尝试优化代码:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class TargetManager : MonoBehaviour
{
   public List<GameObject> targets = new List<GameObject>();

    private int currentTargetIndex = -1;

    private void Start()
    {
        // Desactivar todos los targets al inicio
        foreach (GameObject target in targets)
        {
            target.SetActive(false);
        }

        // Activar un target aleatorio al inicio
        ActivateRandomTarget();
    }

    public void ActivateRandomTarget()
    {
        if (targets.Count == 0)
        {
            Debug.LogWarning("No se encontraron targets. Asegúrate de agregar GameObjects al array de targets en el Inspector.");
            return;
        }

        // Desactivar el target actual
        if (currentTargetIndex >= 0 && currentTargetIndex < targets.Count)
        {
            targets[currentTargetIndex].SetActive(false);
        }

        // Obtener un índice aleatorio del array
        int randomIndex = Random.Range(0, targets.Count);
        // Activar el nuevo target aleatorio
        targets[randomIndex].SetActive(true);

        // Actualizar el índice del target actual
        currentTargetIndex = randomIndex;
    }
}

谢谢!

c# unity-game-engine freeze unityscript
© www.soinside.com 2019 - 2024. All rights reserved.