为什么我的脚本挂当我运行计时器回调方法?团结c#

本文关键字:方法 回调 团结 计时器 运行 脚本 我的 为什么 | 更新日期: 2023-09-27 18:01:32

为什么我的脚本挂当我运行定时器?当计时器停止时,它将回调该方法。

例如我下面的代码:

脚本文件- LakeSpot.cs(此脚本是随机生成的比如Easy Spot, Very Easy Spot和soon)

我缩短了代码并删除了相同的代码

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Linq;
using System.Collections.Generic;
public class LakeSpot : MonoBehaviour {
    player Player;
    fishingDatabase fishDatabase;
    Image itemImage;
    Sprite icon;
    int maxSpot = 5;
    List<spawnSpot> spot = new List<spawnSpot>();
    int k = 0;
    int maxVES;
    int index;
    getSpotLakeScript spotscript;
    // Use this for initialization
    void Start () {
        Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
        fishDatabase = GameObject.FindGameObjectWithTag ("fishingDatabase").GetComponent<fishingDatabase> ();
        index = fishDatabase.spawnRateLake.FindIndex (j => j.Level == spawnSpot.spotLevel.VES);
        maxVES = fishDatabase.spawnRateLake [index].maxSpawn;

            GenerateSpot ();
    }
    // Update is called once per frame
    void Update () {
    }
    public void GenerateSpot() {
            Player.lakerollFishing = new List<int> ();
            Player.lakeSpotRoll = new List<int> ();
            Player.lakeNotActiveSpot = new List<int> ();
        for(int i = 0; i < maxSpot; i++) {
            int roll = Random.Range(0,fishDatabase.spawnRateLake.Count);
            if(fishDatabase.spawnRateLake[roll].Level == spawnSpot.spotLevel.VES) {
                if(maxVES > 0) {
                    this.gameObject.transform.GetChild(i).gameObject.SetActive(true);
                    icon = Resources.Load<Sprite> ("spotLevel" + "/" + "Very Easy Spot");
                    itemImage = this.gameObject.transform.GetChild(i).GetComponent<Image>();
                    itemImage.sprite = icon;
                    maxVES--;
                    Player.lakerollFishing.Add(roll);
                    Player.lakeSpotRoll.Add(i);
                } else {
                    i--;
                }
            }
        }
    }
}

generatspot()方法用于生成随机点,如Very Easy spot, Easy spot和soon

然后我有脚本文件- LakeVisitTimer.cs(这个脚本是倒计时的计时器。当Timer为0时,它会调用

我已经缩短了代码

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
public class LakeVisitTimer : MonoBehaviour {
    public Text TimerText;
    public string dy;
    public float days;
    public float Hours;
    public float Minutes;
    public float Seconds;
    int code;
    LakeSpot spots;
    player Player;
    GameObject lakeside;
    // Use this for initialization
    void Start () { 
        StartCoroutine(Wait());
    }
    void Awake() {
        Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
        lakeside = GameObject.Find ("LakeSide").gameObject;
        spots = lakeside.gameObject.transform.GetChild (1).GetComponent<LakeSpot>();
        code = PlayerPrefs.GetInt ("LakeVisitCode");
        if (code == 1) {
            OnResumeSession ();
        }
    }
    public void StopTimer() {
        Seconds = 0;
        Minutes = 0;
        Hours = 0;
        days = 0;
        Player.maxLakeFishing = 2;
        Player.lakerollFishing = new List<int> ();
        Player.lakeSpotRoll = new List<int> ();
        Player.lakeNotActiveSpot = new List<int> ();
        PlayerPrefs.SetInt("LakeVisitCode",0);
        spots.GenerateSpot ();
    }
}

方法StopTimer()工作时,定时器为零。它回调了spots.GenerateSpot(),这是一个来自LakeSpot.cs的方法,用于生成随机的斑点。

例如,计时器现在为零,它回调GenerateSpot()。当回调时,它变成Hang。

我检查任务管理器的内存占用到300 MB。

为什么我的脚本挂当我运行计时器回调方法?团结c#

我找到解决办法了。

我刚刚删除了脚本:

Player.lakerollFishing = new List<int> ();
 Player.lakeSpotRoll = new List<int> ();
 Player.lakeNotActiveSpot = new List<int> ();

from LakeSpot.cs at GenerateSpot() method

我已经在方法StopTimer()中将该脚本命名为LakeVisitTimer.cs。