当我运行计时器以回调方法时,为什么我的脚本会挂起? Unity C#

时间:2016-08-10 04:51:36

标签: c# unity3d

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

例如我的代码如下:

  

脚本文件 - LakeSpot.cs(此脚本随机生成   像Easy Spot,Very Easy Spot,很快)

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

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--;
                }
            }

        }
    }

}

方法GenereratSpot()用于生成随机点像非常容易点,轻松点,很快

  

然后我有脚本文件 - LakeVisitTimer.cs(这个脚本是   倒计时器。当Timer为零时,它将调用   再次生成Spot()方法。

我缩短了代码。

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()在Timer为零时起作用。它回调了spot.GenerateSpot(),这是一个来自LakeSpot.cs的方法,其中函数用于生成随机点。

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

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

发生了什么事?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

我刚删除了脚本:

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

来自LakeSpot.cs的GenerateSpot()方法

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