在更改场景时获取文本,然后在进入另一个场景时销毁

时间:2016-12-22 02:31:23

标签: c# unity3d

我正在为视力受损的游戏玩家创造一款游戏。我的乐谱文本目前显示在游戏过程中。一旦你输了它就会加载一个新的场景(结束屏幕),但是我想要在加载结束场景时保持分数,然后在再次加载游戏画面时将其重置为0,如果玩家决定返回则将其删除到主菜单。

这是加载下一个场景的原因。

public int amount;
public void ChangeScene(int changeTheScene)        

{
    SceneManager.LoadScene(changeTheScene);
}

void Start()
{

}
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        amount += 1;
        Debug.Log(amount);
        if (amount == 10)      
        {
            SceneManager.LoadScene(2); 
        }
    }
}
}

这就是我追踪得分的方式

void Start()
{
    count = 0;
    SetCountText();
    float x = Random.Range(325f, -600f);
    float y = Random.Range(250f, -450f);
    Debug.Log(x + "," + y);
    prefab.GetComponent<RectTransform>().anchoredPosition = new Vector2(x,    y);
    loseObject.GetComponent<Lose>().amount = 0;
}

public void move()
{
    float x = Random.Range(325f, -600f);
    float y = Random.Range(250f, -450f);
    Debug.Log(prefab.transform.position.x + "," +       prefab.transform.position.y);
    prefab.GetComponent<RectTransform>().anchoredPosition = new Vector2(x,  y);
    loseObject.GetComponent<Lose>().amount = 0;
    count = count + 1;
    SetCountText();
}


void SetCountText()
{
    countText.text = "Count: " + count.ToString();
}
}

1 个答案:

答案 0 :(得分:0)

我不完全确定这是否有用,但为了回答你的问题,我猜这会做你喜欢的事。

using UnityEngine;

//This would be attached to said gameobject that displays the amount.
public class Amount : MonoBehaviour
{
    public static Amount instance = null;

    void Awake()
    {
            if (instance == null)
                instance = this;

            if (instance != this)
                Destroy(gameObject);

            DontDestroyOnLoad(gameObject);
    }
}

//Here would be what's needed to set the amount's parent.
public class SetThatAmountsParentToThisGameObject : MonoBehaviour
{
    void SetAmountParent()
    {
        Amount.instance.transform.SetParent(transform);
    }
}

//And here you would destroy said gameobject that displays the amount.
public class DestroyThatAmount : MonoBehaviour
{
    void DestroyIt()
    {
        Destroy(Amount.instance.gameObject);
    }
}

请注意,我还没有对此进行测试,因此我不确定这是否真的有效。我把它写成答案,因为你问了一个例子,代码更清晰,更容易阅读答案。