[英]Get value from other script file variable Unity C#
我有一個奇怪的問題。 我想從其他腳本文件中獲取價值,但實際上這很容易。 但是這一次我有另一種情況。
例如:
我有player.cs
用數據類型字典保存數據
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class player : MonoBehaviour {
public Dictionary <string, Dictionary <string, int> > product;
}
然后我有margaretShop.cs
來設置字典產品的值
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
調用它
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”吧?
錯誤是“ Object reference not set to an instance of an object
為 Object reference not set to an instance of an object
我已經在margaretSellScript.cs中這樣稱呼gameobject:
mShop = GameObject.FindGameObjectWithTag ("MargaretShop").GetComponent<margaretShop> ();
Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
任何想法?
但是我看到它是完好無損的。 直到我按下按鈕,我才將其設置為活動狀態。
那就是問題所在。 必須enabled
該組件才能enabled
GetComponent<Script> ()
找到它。 默認從編輯器Enable
它。 游戲開始時獲取reference
然后將其disable
。
mShop = GameObject.FindGameObjectWithTag("MargaretShop").GetComponent<margaretShop>();
mShop.enabled = false; //disable it
如果您有在margaretShop
腳本的Start()
函數中運行的任何代碼,則可以將其刪除,並將其放在名為initScript()
的自定義公共函數中。
現在,當您按下Button
,可以使用mShop.enabled = false;
激活它mShop.enabled = false;
,然后調用initScript()
函數在margaretShop
腳本中初始化變量。 而已。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.