unity场景更改后如何重置分数?

时间:2020-06-27 14:43:49

标签: c# unity3d

当我将游戏放到现场时,在开始新游戏时分数仍然保留:

ScoringSYstem.cs

    public GameObject scoreText;

    public static int theScore;

    void Update()
    {   
        scoreText.GetComponent<Text>().text = "Score: " + theScore; 
    }

Timer.cs

   public string LevelToLoad;
 public static float timer1 = 30f;
 private Text timerSeconds;

    public GameObject scoreText;

    public static int theScore;


 // Use this for initialization
 void Start () 
 {
  timerSeconds = GetComponent<Text> ();
 }
 
 // Update is called once per frame
 void Update () 
 {
  timer1 -= Time.deltaTime;
  timerSeconds.text = timer1.ToString("f0");
  if (timer1 <= 0) 
     {    
        timer1 = 30f;    
        Application.LoadLevel (LevelToLoad); 

 }
}

如何在场景变化时重置分数?

2 个答案:

答案 0 :(得分:0)

首先,您需要创建一个称为GameManager的空GameObject,然后向其中添加一个也称为GameManager的脚本。这样您就可以从任何地方访问您的分数。

public int score = 0;
public static int time = 30;   

#region Singelton
public static GameManager instance;

void Awake()
{
   if (instance != null)
   {
      Debug.LogWarning("More than one Instance of Inventory found");
      return;
   }

   instance = this;
}
#endregion

public void GameOver()
{
   score = 0;
   scoreText.GetComponent<Text>().text = "Score: " + gm.score;
}

然后您可以从任何地方调用这些变量并进行更改:

GameManager gm;

void Start()
{
   gm = GameManager.instance;
}

void Update()
{
   if (time >= 0)
      gm.GameOver();
}

答案 1 :(得分:0)

您可以使用

$ valgrind /bin/ls

==2116== Memcheck, a memory error detector
==2116== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==2116== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==2116== Command: /bin/ls
==2116==
--2116-- WARNING: Serious error when reading debug info
--2116-- When reading debug info from /lib/arm-linux-gnueabihf/ld-2.28.so:
--2116-- Ignoring non-Dwarf2/3/4 block in .debug_info
--2116-- WARNING: Serious error when reading debug info
--2116-- When reading debug info from /lib/arm-linux-gnueabihf/ld-2.28.so:
--2116-- Last block truncated in .debug_info; ignoring
==2116== Conditional jump or move depends on uninitialised value(s)
==2116==    at 0x401A5D0: index (in /lib/arm-linux-gnueabihf/ld-2.28.so)
==2116==
==2116== Conditional jump or move depends on uninitialised value(s)
==2116==    at 0x401A5D4: index (in /lib/arm-linux-gnueabihf/ld-2.28.so)
==2116==
==2116== Conditional jump or move depends on uninitialised value(s)
==2116==    at 0x4008040: _dl_dst_count (in /lib/arm-linux-gnueabihf/ld-2.28.so)
==2116==
==2116== Conditional jump or move depends on uninitialised value(s)
==2116==    at 0x4008288: expand_dynamic_string_token (in /lib/arm-linux-gnueabihf/ld-2.28.so)
==2116==
==2116== Conditional jump or move depends on uninitialised value(s)
==2116==    at 0x401AA80: strlen (in /lib/arm-linux-gnueabihf/ld-2.28.so)
==2116==
==2116== Conditional jump or move depends on uninitialised value(s)
==2116==    at 0x401AA84: strlen (in /lib/arm-linux-gnueabihf/ld-2.28.so)
==2116==
==2116== Conditional jump or move depends on uninitialised value(s)
==2116==    at 0x4017F68: malloc (in /lib/arm-linux-gnueabihf/ld-2.28.so)    
...

如果GameOver场景使用某种GameEngineObject,则Gameover场景中的方法会重置保存该变量的GameObject中的Score变量。

Unity Doc SceneManager