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.
For example:
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;
}
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();
}
}
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"));
}
}
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?
The error is Object reference not set to an instance of an object
I have already call the gameobject like this at margaretSellScript.cs :
mShop = GameObject.FindGameObjectWithTag ("MargaretShop").GetComponent<margaretShop> ();
Player = GameObject.FindGameObjectWithTag ("player").GetComponent<player> ();
Any Idea?
But i see it is attacted. Just i am not set it active until i press the button.
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
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()
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.