2017-07-17 93 views
0

好吧,我有一個'迷你遊戲',不能正常工作。 基本上,我的ClickedStartButton()只有一半的工作。 如果我按下play並點擊開始,一切都完美無缺。 但是,如果我改變場景,然後回到主菜單並點擊播放停止工作。完全沒有任何反應。我不明白由於場景被重新加載,我的代碼的哪一部分會導致此問題。我知道ClickedStartButton()被調用,因爲isRolling = true;叫做。但是,好像這是在場景重新加載之後從函數調用的唯一東西。因爲不是我的硬幣滾動通過什麼都沒有發生。Unity3D功能退出場景變換後工作

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using System; 

public class chestScript : MonoBehaviour { 

    public float msToWait = 10800000.0f; // Wait time for Chest. 
    private const float ROLL_OFFSET = 650.0f; 
    public GameObject chestTimer; 
    private Button chestButton; 
    public Button startButton; 
    private Text chestTimerTxt; 
    public GameObject chestTimerBg; 
    private ulong lastChestOpen; 
    public GameObject newGift; 

    public Text coinTxt; 

    public Transform RollContainer; 
    private Transform[] rolls; 
    public GameObject rewardPanels; 

    private int coinAmt; 

    private bool isInitialized; 

    private bool isRolling; 
    private float transition; 
    private string giftName; 

    private int playerCoins; 

    private void Start() 
    { 
     isRolling = false; 
     InitializeGiftbox(); 
    } 
    private void InitializeGiftbox() 
    { 
     rewardPanels.SetActive (false); 

     coinAmt = 0; 

     chestButton = GetComponent<Button>(); 
     lastChestOpen = ulong.Parse(PlayerPrefs.GetString ("LastChestOpen")); 
     chestTimerTxt = GetComponentInChildren<Text>(); 
     rewardPanels.SetActive (false); 
     if (!IsChestReady()) { 
      startButton.interactable = false; 
      chestButton.interactable = false; 
      newGift.SetActive (false); 
      chestTimerBg.SetActive (true); 
      chestTimer.SetActive (true); 
     } 
     rolls = new Transform[RollContainer.childCount]; 
     for (int i = 0; i < RollContainer.childCount; i++) 
      rolls [i] = RollContainer.GetChild (i); 

     isInitialized = true; 
    } 
    private void Update() 
    { 
     if (!chestButton.IsInteractable()) 
     { 
      if (IsChestReady()) { 
       chestButton.interactable = true; 
       startButton.interactable = true; 
       chestTimer.SetActive (false); 
       chestTimerBg.SetActive (false); 
       newGift.SetActive (true); 
       return; 
      } 
      //Set Timer in h:m:s format 
      ulong diff = ((ulong)DateTime.Now.Ticks - lastChestOpen); 
      ulong m = diff/TimeSpan.TicksPerMillisecond; 
      float secondsLeft = (float)(msToWait - m)/1000.0f; 

      string r = " "; 
      //Hours 
      r += ((int)secondsLeft/3600).ToString("00") + " : "; 
      secondsLeft -= ((int)secondsLeft/3600) * 3600; 
      //Minutes 
      r += ((int)secondsLeft/60).ToString("00") + " : "; 
      //Seconds 
      r += ((int)secondsLeft % 60).ToString("00"); 
      chestTimerTxt.text = r; 

     } 
     if (isRolling) 
     { 
      Vector3 end = (-Vector3.right * ROLL_OFFSET) * (rolls.Length - 1); 
      RollContainer.transform.localPosition = Vector3.Lerp (Vector3.right * ROLL_OFFSET, end, transition); 
      transition += Time.deltaTime/5.0f; 
      if (transition > 1) 
      { 
       isRolling = false; 
       playerCoins = PlayerPrefs.GetInt ("coinAmount"); 
       chestButton.interactable = false; 
       startButton.interactable = false; 
       chestTimer.SetActive (true); 
       chestTimerBg.SetActive (true); 
       newGift.SetActive (false); 
       switch (giftName) 
       { 
       case "15Gold": 
        playerCoins += 15; 
        PlayerPrefs.SetInt ("coinAmount", playerCoins); 
        coinTxt.text = playerCoins.ToString(); 
        break; 
       case "5Gold": 
        playerCoins += 5; 
        PlayerPrefs.SetInt ("coinAmount", playerCoins); 
        coinTxt.text = playerCoins.ToString(); 
        break; 
       case "10Gold": 
        playerCoins += 10; 
        PlayerPrefs.SetInt ("coinAmount", playerCoins); 
        coinTxt.text = playerCoins.ToString(); 
        break; 
       case "1000Gold": 
        playerCoins += 1000; 
        PlayerPrefs.SetInt ("coinAmount", playerCoins); 
        coinTxt.text = playerCoins.ToString(); 
        break; 
       case "100Gold": 
        playerCoins += 100; 
        PlayerPrefs.SetInt ("coinAmount", playerCoins); 
        coinTxt.text = playerCoins.ToString(); 
        break; 
       case "40Gold": 
        playerCoins += 40; 
        PlayerPrefs.SetInt ("coinAmount", playerCoins); 
        coinTxt.text = playerCoins.ToString(); 
        break; 
       default: 
       case "Nothing": 
        Debug.Log ("Nothing happened"); 
        break; 
       } 
       PlayerPrefs.Save(); 
      } 
     } 
    } 
    public void ChestClick() 
    {  
     rewardPanels.SetActive (true); 
    } 
    private bool IsChestReady() 
    { 
     ulong diff = ((ulong)DateTime.Now.Ticks - lastChestOpen); 
     ulong m = diff/TimeSpan.TicksPerMillisecond; 
     float secondsLeft = (float)(msToWait - m)/1000.0f; 

     if (secondsLeft < 0) { 
      startButton.interactable = true; 
      chestButton.interactable = true; 
      chestTimer.SetActive (false); 
      chestTimer.SetActive (false); 
      newGift.SetActive (true); 
      return true; 
     } 
     return false; 
    } 
    public void ClickedStartButton() 
    { 
     lastChestOpen = (ulong)DateTime.Now.Ticks; 
     PlayerPrefs.SetString ("LastChestOpen", lastChestOpen.ToString()); 
     transition = 0; 
     float offset = 0.0f; 
     List<int> indexes = new List<int>(); 
     for (int i = 0; i < rolls.Length; i++) 
      indexes.Add (i); 

     for (int i = 0; i < rolls.Length; i++) { 
      int index = indexes [UnityEngine.Random.Range (0, indexes.Count)]; 
      indexes.Remove (index); 
      rolls [index].transform.localPosition = Vector3.right * offset; 
      offset += ROLL_OFFSET; 
      giftName = rolls [index].name; 
     } 
     isRolling = true; 
    } 
    public void ClickedCloseButton() 
    { 
     rewardPanels.SetActive (false); 
    } 
} 
+1

['DontDestroyOnLoad(Object)'](https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html)... –

回答

0

所以我不知道爲什麼,但由於某種原因時標是通過將

Time.timeScale = 1; 

它固定我的問題轉移了在Unity場景之間的話,希望這將幫助其他人誰是按鈕退出在現場變化下工作。簡單的修復。

+0

當然,它「在場景之間轉移」。這是一個*全球價值。* – Draco18s