从其他脚本文件变量Unity C#获取价值 [英] Get value from other script file variable Unity C#

查看:64
本文介绍了从其他脚本文件变量Unity C#获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题.我想从其他脚本文件中获取价值,但实际上它很容易.但是这次我还有另一种情况.

I have a weird question. I want to get value from other script file, but actually it is quick easy. But this time I have an another case.

例如:

我有 player.cs 用数据类型字典保存数据

I have player.cs that save the data with datatype dictionary

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

public class player : MonoBehaviour {

    public Dictionary <string, Dictionary <string, int> > product;

}

然后我有 margaretShop.cs 来设置字典产品的值

then I have margaretShop.cs that set the value of dictionary product

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

public class margaretShop : MonoBehaviour {
    player Player;

    // Use this for initialization
    void Start () {
        Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();

            setValue("A", "id", "10");
            setValue("A", "name", "A");
            setValue("A", "qty", "4");
            setValue("A", "price", "120");



    }


    // Update is called once per frame
    void Update () {

    }

    void init() {
        if (Player.product != null) {
            return;
        }
        Player.product = new Dictionary<string, Dictionary <string, int> > ();
    }

    public int getValue(string nameproduct, string property) {
        init ();

        if (Player.product.ContainsKey (nameproduct) == false) {
            return 0;
        }

        if (Player.product [nameproduct].ContainsKey (property) == false) {
            return 0;
        }

        return Player.product [nameproduct] [property];
    }

    public void setValue(string nameproduct, string property, int value) {
        init ();

        if (Player.product.ContainsKey (nameproduct) == false) {
            Player.product[nameproduct] = new Dictionary<string, int>();

        }

        Player.product [nameproduct] [property] = value; // This store to player.cs file product
    }


    public string[] getProductName() {
        return Player.product.Keys.ToArray();

    }

}

然后最后我用另一个脚本文件 margaretSellScript.cs

and then last I call it with another script file margaretSellScript.cs

using UnityEngine;
using System.Collections;
using System.Linq;
using UnityEngine.UI;

public class margaretSellScript : MonoBehaviour {
    margaretShop mShop;

    player Player;

    // Use this for initialization
    void Start () {
        mShop = GameObject.FindGameObjectWithTag ("MargaretShop").GetComponent<margaretShop> ();
        Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();

    Debug.Log("QTY : " + mShop.getValue ("A", "qty"));

    }                 
}

在此脚本中: Debug.Log("QTY:" + mShop.getValue("A","qty")); 为什么我不能从player.cs产品中获取值可变字典?该值必须为"4" 吗?

In this script: Debug.Log("QTY : " + mShop.getValue ("A", "qty")); why I can't get the value from player.cs product variable dictionary? The value must be "4" right?

错误是 对象引用未设置为对象的实例

我已经在 margaretSellScript.cs 中像这样调用了游戏对象:

I have already call the gameobject like this at margaretSellScript.cs :

mShop = GameObject.FindGameObjectWithTag ("MargaretShop").GetComponent<margaretShop> ();
            Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();

有什么想法吗?

推荐答案

但我看到它已经完成了.只是我直到按下时才将其设置为活动状态按钮.

But i see it is attacted. Just i am not set it active until i press the button.

就是这个问题.该组件必须已启用 才能使用 GetComponent< Script>()进行查找.默认从编辑器中启用.游戏开始时,获取 reference ,然后 disable .

That's the problem. The component must be enabled in order for GetComponent<Script> () to find it. Enable it from the Editor as default. When game starts get the reference then disable it.

mShop = GameObject.FindGameObjectWithTag("MargaretShop").GetComponent<margaretShop>();
mShop.enabled = false; //disable it

如果您有任何代码在 margaretShop 脚本的 Start()函数中运行,则可以将其删除并将其放在名为的自定义公共函数中> initScript().

If you have any code that runs in the Start() function of the margaretShop script, you can remove them and put them in a custom public function named initScript().

现在,当您按下 Button 时,可以使用 mShop.enabled = false; 激活它,然后调用 initScript()函数在 margaretShop 脚本中初始化变量.就是这样.

Now, when you press the Button, you can activate it with mShop.enabled = false;, then call the initScript() function to initialize your variables in the margaretShop script. That's it.

这篇关于从其他脚本文件变量Unity C#获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆