C# Unity3D功能在场景更改后停止工作

C# Unity3D功能在场景更改后停止工作,c#,android,unity3d,C#,Android,Unity3d,好吧,我有一个小游戏,它不能正常工作。 基本上,我的ClickedStartButton()只工作了一半。 如果我按play并点击start,一切都会完美运行。 但是,如果我更改了场景,然后返回主菜单并单击“播放”,它将停止工作。什么也没发生。我不明白我的代码的哪一部分会导致这个问题,因为场景被重新加载。我知道正在调用ClickedStartButton(),因为isRolling=true;被称为。然而,似乎这是在场景重新加载后从函数调用的唯一内容。因为我的硬币没有滚动,什么也没有发生 usi

好吧,我有一个小游戏,它不能正常工作。 基本上,我的ClickedStartButton()只工作了一半。 如果我按play并点击start,一切都会完美运行。 但是,如果我更改了场景,然后返回主菜单并单击“播放”,它将停止工作。什么也没发生。我不明白我的代码的哪一部分会导致这个问题,因为场景被重新加载。我知道正在调用ClickedStartButton(),因为isRolling=true;被称为。然而,似乎这是在场景重新加载后从函数调用的唯一内容。因为我的硬币没有滚动,什么也没有发生

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);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用制度;
公共类脚本:单行为{
公共浮点数msToWait=10800000.0f;//等待胸部的时间。
专用常数浮动辊偏移=650.0f;
公共游戏对象;
私人按钮;
公共按钮开始按钮;
私有文本ChestTimerText;
公共游戏对象;
乌龙私人酒店;
公共游戏对象新礼物;
公共文本;
公共容器;
私有转换[]卷;
公开游戏对象奖励面板;
私有货币;
私有资产初始化;
私人厕所;
私有浮动过渡;
私有字符串giftName;
私人国际游戏中心;
私有void Start()
{
isRolling=false;
初始化giftbox();
}
私有void InitializeGiftbox()
{
rewardPanels.SetActive(假);
货币金额=0;
chestButton=GetComponent();
lastChestOpen=ulong.Parse(PlayerPrefs.GetString(“lastChestOpen”);
chestTimerText=getComponentChildren();
rewardPanels.SetActive(假);
如果(!IsChestReady()){
startButton.interactiable=false;
chestButton.interactiable=false;
newGift.SetActive(false);
chestTimerBg.SetActive(真);
chestTimer.SetActive(真);
}
rolls=新转换[RollContainer.childCount];
for(int i=0;i1)
{
isRolling=false;
playerCoins=PlayerPrefs.GetInt(“硬币金额”);
chestButton.interactiable=false;
startButton.interactiable=false;
chestTimer.SetActive(真);
chestTimerBg.SetActive(真);
newGift.SetActive(false);
开关(giftName)
{
案例“15黄金”:
playerCoins+=15;
PlayerPrefs.SetInt(“币量”,playerCoins);
coinTxt.text=playerCoins.ToString();
打破
案例“5Gold”:
playerCoins+=5;
PlayerPrefs.SetInt(“币量”,playerCoins);
coinTxt.text=playerCoins.ToString();
打破
案例“10黄金”:
playerCoins+=10;
PlayerPrefs.SetInt(“币量”,playerCoins);
coinTxt.text=playerCoins.ToString();
打破
案例“1000黄金”:
playerCoins+=1000;
PlayerPrefs.SetInt(“币量”,playerCoins);
coinTxt.text=playerCoins.ToString();
打破
案例“100黄金”:
playerCoins+=100;
PlayerPrefs.SetInt(“币量”,playerCoins);
coinTxt.text=playerCoins.ToString();
打破
案例“40黄金”:
playerCoins+=40;
PlayerPrefs.SetInt(“币量”,playerCoins);
coinTxt.text=playerCoins.ToString();
打破
违约:
“无”一案:
Log(“什么也没发生”);
打破
}
保存();
}
}
}
公共空间单击()
{       
rewardPanels.SetActive(真);
}
私人布尔伊斯切斯特雷迪()
{
ulong diff=((ulong)DateTime.Now.Ticks-lastChestOpen);
ulong m=差/时间跨度。毫秒;
第二浮球浮球高度=(浮球)(msToWait-m)/1000.0f;
如果(秒英尺<0)
Time.timeScale = 1;