2016-08-10 20 views
0

爲什麼我的腳本在我運行定時器時掛起?當定時器停止時,它將回調該方法。爲什麼我的腳本在我運行計時器回調方法時掛起? Unity C#

例如下面我的代碼:

腳本文件 - LakeSpot.cs(這個腳本是隨機生成的斑點 一樣簡單點,非常容易點,很快)

我縮短了代碼並刪除了相同的代碼。

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(此腳本 倒計時計時器。當定時器是零,這將再次調用 GenerateSpot()方法。

我有縮短的代碼。

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爲零時工作。並且它會調用spots.GenerateSpot(),它是LakeSpot.cs中的一個方法,其中函數用於隨機生成點。

例如,計時器現在爲零,並回調GenerateSpot()。當回電時它變成了Hang。

我檢查任務管理器的內存,直到300 MB。

這是怎麼回事?

+0

**「當我運行我的定時器時腳本掛起」**運行定時器代碼在哪裏?如果你不知道如何使用調試器並告訴我們腳本在哪裏凍結,爲什麼不把'Debug.Log'放在任何地方。它在某處,但沒有人知道。 – Programmer

+0

嗨@Programmer,腳本計時器代碼位於LakeVisitTimer.cs中。這是一個腳本計時器。但是我縮短了代碼。我只是告訴你回調方法 –

+0

@Programmer,我想我已經找到了問題。問題出現在generateSpot()中的LakeSpot.cs腳本中:Player.lakerollFishing = new List (); \t \t Player.lakeSpotRoll = new List (); \t \t Player.lakeNotActiveSpot = new List ();並且在StopTimer()方法中的LakeVisitTimer.cs腳本中也有該腳本。當我從LakeSpot.cs中刪除它時,它在GenerateSpot()方法中運行成功。我認爲問題在那裏。現在它工作 –

回答

0

我找到了解決方案。

我只是刪除了腳本:

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

從LakeSpot.cs在GenerateSpot()方法

這劇本我已經把它LakeVisitTimer.cs的方法StopTimer()。

相關問題