2016-08-10 6 views
0

私のタイマーを実行すると、スクリプトがハングアップするのはなぜですか?タイマーが停止したら、メソッドを呼び出します。例えばメソッドをコールバックするタイマを実行すると、スクリプトがハングするのはなぜですか? Unity C#

以下の私のコード:

スクリプトファイル - LakeSpot.cs(このスクリプトはすぐに簡単にスポット、非常に簡単なスポットのようにランダムなスポット を生成し、かつある)

Iコードを短縮し、同じコードを削除してください。

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

     } 
    } 

} 
Methodeの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()タイマがゼロのときの仕事です。そして、それはスポットをコールバックします.GenerateSpot()は、LakeSpot.csからのメソッドで、関数はランダムに生成します。

たとえば、タイマーは現在ゼロで、GenerateSpot()をコールバックします。コールバックするとハングします。

タスクマネージャを確認して、メモリを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

解決策が見つかりました。

は、私はちょうどスクリプト削除:私はすでにそれを呼び出すこのスクリプトは、メソッドは、stopTimerでLakeVisitTimer.cs GenerateSpot()メソッド

でLakeSpot.csから

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

を()。

関連する問題