简体   繁体   English

在场景之间统一更改变量

[英]Change variables between scenes unity

I have a scene where I write down my player stats. 我有一个场景,记下我的球员数据。 In the next scene (basically in the next 2 scenes but it doesn't matter) I want to buy some weapons and change the variables. 在下一个场景中(基本上在接下来的两个场景中,但这没关系),我想购买一些武器并更改变量。

The thing is I'm saving the object with "DontDestroyOnLoad" and when I go to the next scene I want to find out how can I change the variables. 事情是我用“ DontDestroyOnLoad”保存对象,当我转到下一个场景时,我想知道如何更改变量。

First Scene: 第一个场景:

在此处输入图片说明

The code: 编码:

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

public class statsTextsToDisplayAndChanges : MonoBehaviour
{
    public static statsTextsToDisplayAndChanges _INstance;
    buyWeapons bw;
    public Text m_LevelText;
    public Text m_AttackText;
    public Text m_DefendText;
    public Text m_MoneyText;
    public int level = 0;
    public int money = 500;
    public int health = 400;
    public int attack = 10;
    public int def = 5;
    string answer;
    string url = "http://alex3685.dx.am/display.php";


    public int sword1 = 200;


    public void Start()
    {

        _INstance = this;
        DontDestroyOnLoad(this.gameObject);

        display();

    }

    public void display()
    {


        m_LevelText.text = level.ToString();
        m_AttackText.text = attack.ToString();
        m_DefendText.text = def.ToString();
        m_MoneyText.text = money.ToString();
    }

    public void onPurchase()
    {
        if (money >= sword1)
        {
            Debug.Log("YOU BOUGHT IT");
            money -= sword1;
            //Destroy(GameObject.Find("playerStats"));
            display();
        }
    }

}

Second scene: 第二场景:

在此处输入图片说明

When I press the button the debug.log from the purchase function works but in the text nothing changes (from 500 coins-200 coins of the sword=300 coins). 当我按下按钮时,购买功能的debug.log起作用了,但是文本中没有任何变化(从500枚硬币到200枚剑的硬币= 300枚硬币)。

在此处输入图片说明

Any ideas? 有任何想法吗?

Thanks in advance. 提前致谢。

Hmmm....initial thoughs: 嗯。。。

This is one of the times where you want a true manager object with a scrip attached that only knows how to store the relevant stat. 这是您想要一个真正的管理器对象并附有仅能存储相关统计信息的脚本的情况之一。

Then, in whatever scripts that handles the purchasing they have a private GameObject with a reference to the manager object. 然后,在处理购买的任何脚本中,它们都有一个私有GameObject并引用管理器对象。 private GameObject _Manager;

But the object exists in another scene in my editor so I cant drag and drop the reference! 但是该对象存在于我的编辑器的另一个场景中,因此我无法拖放引用!

No problem, Lets say that the object with the component that stores the stats are called "PlayerManager" . 没问题,可以说具有存储统计信息的组件的对象称为"PlayerManager" In the script that handles the purchasing in the start() method add: _Manager = gameobject.Find("PlayerManager"); 在通过start()方法处理购买的脚本中,添加: _Manager = gameobject.Find("PlayerManager");

Now you can change all the variables to your hearts content, assuming of course that you have set the variables in the manager to be public, or if you are more advanced and concerned about robust code, you have the proper get;set; 现在,您可以更改所有变量以适应您的内心需求,当然,假设您已将管理器中的变量设置为公共变量,或者如果您更高级并且关注健壮的代码,则可以使用正确的get; set;方法。 methods in place. 方法到位。

Hope this helped! 希望这对您有所帮助!

Use PlayerPrefs to save and retrieve data. 使用PlayerPrefs保存和检索数据。

Source : https://docs.unity3d.com/ScriptReference/PlayerPrefs.html 来源: https : //docs.unity3d.com/ScriptReference/PlayerPrefs.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM