Access To a Variable of another script which in another scene
Hey There iam trying to get a variable from another script which is in another scene
The Goal here that there is 2 scenes the first scene there is a Money Value which is named AmericaFire and the script that include the money value is attached to the main camera and this is the code:
public int AmericaFire;
//Tank
public int Tank_Amount;
public Texture Tank;
//F16
public int F16_Amount;
public Texture F16;
//Rafael
public int Rafael_Amount;
public Texture Rafael;
//LongMissile
public int LongMissile_Amount;
public Texture Long_Missile;
//AntiPlaneMissile
public int AntiPlaneMissile_Amount;
public Texture AntiPlane_Missile;
//ReflecteHit
public int ReflecteHit_Amount;
public Texture Reflecte_Hit;
//DefendHit
public int DefendHit_Amount;
public Texture Defend_Hit;
// Use this for initialization
void Start () {
//AmericaFire
PlayerPrefs.GetInt ("AmericaFirePref");
AmericaFire = PlayerPrefs.GetInt ("AmericaFirePref");
//AmericaTank
PlayerPrefs.GetInt ("AmericaTankPref");
Tank_Amount = PlayerPrefs.GetInt ("AmericaTankPref");
//AmericaF16
PlayerPrefs.GetInt ("AmericaF16Pref");
F16_Amount = PlayerPrefs.GetInt ("AmericaF16Pref");
//AmericaRafael
PlayerPrefs.GetInt ("AmericaRafaelPref");
Rafael_Amount = PlayerPrefs.GetInt ("AmericaRafaelPref");
//AmericaLongMissile
PlayerPrefs.GetInt ("AmericaLongMissilePref");
LongMissile_Amount = PlayerPrefs.GetInt ("AmericaLongMissilePref");
//AmericaAntiPlaneMissile
PlayerPrefs.GetInt ("AmericaAntiPlaneMissilePref");
AntiPlaneMissile_Amount = PlayerPrefs.GetInt ("AmericaAntiPlaneMissilePref");
//AmericaReflecteHit
PlayerPrefs.GetInt ("AmericaReflecteHitPref");
ReflecteHit_Amount = PlayerPrefs.GetInt ("AmericaReflectePref");
//AmericaDefendHit
PlayerPrefs.GetInt ("AmericaDefendHitPref");
DefendHit_Amount = PlayerPrefs.GetInt ("AmericaDefendHitPref");
}
// Update is called once per frame
void Update () {
//AmericaFire
PlayerPrefs.SetInt ("AmericaFirePref", AmericaFire);
//AmericaF16
PlayerPrefs.SetInt ("AmericaF16Pref", F16_Amount);
//AmericaRafael
PlayerPrefs.SetInt ("AmericaRafaelPref", Rafael_Amount);
//AmericaLongMissile
PlayerPrefs.SetInt ("AmericaLongMissilePref", LongMissile_Amount);
//AmericaAntiPlaneMissile
PlayerPrefs.SetInt ("AmericaAntiPlaneMissilePref", AntiPlaneMissile_Amount);
//AmericaReflecteHit
PlayerPrefs.SetInt ("AmericaReflecteHitPref", ReflecteHit_Amount);
//AmericaDefendHit
PlayerPrefs.SetInt ("AmericaDefendHitPref", DefendHit_Amount);
//AmericaTank
PlayerPrefs.SetInt ("AmericaTankPref", Tank_Amount);
}
void OnGUI () {
//Back
if (GUI.Button (new Rect (1225, 0, 50, 40), "Back")) {
Application.LoadLevel("Countres_Screen");
}
//Attack
if (GUI.Button (new Rect (0, 0, 80, 80), "Attack")) {
Application.LoadLevel("AmericaWar");
}
//Fire
GUI.Label (new Rect (390, 250, 120, 120), "Fire:");
GUI.Label (new Rect (390, 270, 120, 120), "" + AmericaFire);
//Tank
GUI.DrawTexture (new Rect (500, 250, 150, 120), Tank);
GUI.TextArea (new Rect (650, 250, 150, 40), "Tank it's a War Vehicle armed with HeavyGun");
GUI.Label (new Rect (650, 290, 120, 120), "Price: 300,000F");
GUI.Label (new Rect (650, 320, 120, 120), "haggling: 30,000F");
GUI.Label (new Rect (650, 305, 120, 120), "Damage: 60,000F");
GUI.Label (new Rect (570, 370, 120, 120), "Amount:");
GUI.Label (new Rect (620, 370, 120, 120), "" + Tank_Amount);
if (GUI.Button (new Rect (650, 340, 50, 40), "Buy")) {
if(AmericaFire < 300000) {
}
else if(AmericaFire >= 300000) {
AmericaFire -= 300000;
Tank_Amount += 1;
}
}
if (GUI.Button (new Rect (700, 340, 50, 40), "Sell")) {
if(Tank_Amount <= 0) {
}
if(Tank_Amount > 0) {
Tank_Amount -= 1;
AmericaFire += 300000;
}
}
//F16
GUI.DrawTexture (new Rect (500, 460, 150, 120), F16);
GUI.TextArea (new Rect (650, 435, 150, 60), "F16 it's a powerful WarShip it can deal massive damage");
GUI.Label (new Rect (650, 500, 180, 120), "Price: 4,500,000F");
GUI.Label (new Rect (650, 530, 180, 120), "haggling: 2,000,000F");
GUI.Label (new Rect (650, 515, 180, 120), "Damage: 3,000,000F");
GUI.Label (new Rect (570, 580, 120, 120), "Amount:");
GUI.Label (new Rect (620, 580, 120, 120), "" + F16_Amount);
if (GUI.Button (new Rect (650, 550, 50, 40), "Buy")) {
if(AmericaFire < 4500000) {
}
else if(AmericaFire >= 4500000) {
AmericaFire -= 4500000;
F16_Amount += 1;
}
}
if (GUI.Button (new Rect (700, 550, 50, 40), "Sell")) {
if(F16_Amount <= 0) {
}
if(F16_Amount > 0) {
F16_Amount -= 1;
AmericaFire += 4500000;
}
}
//Rafael
GUI.DrawTexture (new Rect (850, 460, 150, 120), Rafael);
GUI.TextArea (new Rect (1000, 435, 150, 65), "Rafael this WarShip is super power armed with powerful missiles");
GUI.Label (new Rect (1000, 500, 180, 120), "Price: 3,000,000F");
GUI.Label (new Rect (1000, 530, 180, 120), "haggling: 300,000F");
GUI.Label (new Rect (1000, 515, 180, 120), "Damage: 600,000F");
GUI.Label (new Rect (920, 580, 120, 120), "Amount:");
GUI.Label (new Rect (970, 580, 120, 120), "" + Rafael_Amount);
if (GUI.Button (new Rect (1000, 550, 50, 40), "Buy")) {
if(AmericaFire < 3000000) {
}
else if(AmericaFire >= 3000000) {
AmericaFire -= 3000000;
Rafael_Amount += 1;
}
}
if (GUI.Button (new Rect (1050, 550, 50, 40), "Sell")) {
if(Rafael_Amount <= 0) {
}
if(Rafael_Amount > 0) {
Rafael_Amount -= 1;
AmericaFire += 3000000;
}
}
//LongMissiles
GUI.DrawTexture (new Rect (850, 250, 150, 120), Long_Missile);
GUI.TextArea (new Rect (1000, 250, 170, 40), "LongMissiles consider from the most powerful missiles");
GUI.Label (new Rect (1000, 290, 120, 120), "Price: 1,000,000F");
GUI.Label (new Rect (1000, 320, 120, 120), "haggling: 100,000F");
GUI.Label (new Rect (1000, 305, 120, 120), "Damage: 200,000F");
GUI.Label (new Rect (920, 370, 120, 120), "Amount:");
GUI.Label (new Rect (970, 370, 120, 120), "" + LongMissile_Amount);
if (GUI.Button (new Rect (1000, 340, 50, 40), "Buy")) {
if(AmericaFire < 1000000) {
}
else if(AmericaFire >= 1000000) {
AmericaFire -= 1000000;
LongMissile_Amount += 1;
}
}
if (GUI.Button (new Rect (1050, 340, 50, 40), "Sell")) {
if(LongMissile_Amount <= 0) {
}
if(LongMissile_Amount > 0) {
LongMissile_Amount -= 1;
AmericaFire += 1000000;
}
}
//AntiPlaneMissile
GUI.DrawTexture (new Rect (850, 70, 150, 120), AntiPlane_Missile);
GUI.TextArea (new Rect (1000, 70, 280, 40), "AntiPlaneMissile 3 of them can breakdown F16 and 5 of them can breakdown Rafael");
GUI.Label (new Rect (1000, 110, 120, 120), "Price: 500,000F");
GUI.Label (new Rect (920, 190, 120, 120), "Amount:");
GUI.Label (new Rect (970, 190, 120, 120), "" + AntiPlaneMissile_Amount);
if (GUI.Button (new Rect (1000, 135, 50, 40), "Buy")) {
if(AmericaFire < 500000) {
}
else if(AmericaFire >= 500000) {
AmericaFire -= 500000;
AntiPlaneMissile_Amount += 1;
}
}
if (GUI.Button (new Rect (1050, 135, 50, 40), "Sell")) {
if(AntiPlaneMissile_Amount <= 0) {
}
if(AntiPlaneMissile_Amount > 0) {
AntiPlaneMissile_Amount -= 1;
AmericaFire += 500000;
}
}
//DefendHit
GUI.DrawTexture (new Rect (500, 70, 150, 120), Defend_Hit);
GUI.TextArea (new Rect (650, 70, 200, 40), "Defend Hit one of this can cancel an attack and destroy the weapon");
GUI.Label (new Rect (650, 110, 120, 120), "Price: 2,000,000F");
GUI.Label (new Rect (570, 190, 120, 120), "Amount:");
GUI.Label (new Rect (620, 190, 120, 120), "" + DefendHit_Amount);
if (GUI.Button (new Rect (650, 135, 50, 40), "Buy")) {
if(AmericaFire < 2000000) {
}
else if(AmericaFire >= 2000000) {
AmericaFire -= 2000000;
DefendHit_Amount += 1;
}
}
if (GUI.Button (new Rect (700, 135, 50, 40), "Sell")) {
if(DefendHit_Amount <= 0) {
}
if(DefendHit_Amount > 0) {
DefendHit_Amount -= 1;
AmericaFire += 2000000;
}
}
//ReflecteHit
GUI.DrawTexture (new Rect (0, 250, 150, 120), Reflecte_Hit);
GUI.TextArea (new Rect (150, 250, 200, 60), "Reflecte Hit one of this can Reflecte the attack to the other team and deal the same damage");
GUI.Label (new Rect (150, 310, 120, 120), "Price: 3,500,000F");
GUI.Label (new Rect (40, 370, 120, 120), "Amount:");
GUI.Label (new Rect (90, 370, 120, 120), "" + ReflecteHit_Amount);
if (GUI.Button (new Rect (150, 330, 50, 40), "Buy")) {
if(AmericaFire < 3500000) {
}
else if(AmericaFire >= 3500000) {
AmericaFire -= 3500000;
ReflecteHit_Amount += 1;
}
}
if (GUI.Button (new Rect (200, 330, 50, 40), "Sell")) {
if(ReflecteHit_Amount <= 0) {
}
if(ReflecteHit_Amount > 0) {
ReflecteHit_Amount -= 1;
AmericaFire += 3500000;
}
}
}
and in the another scene there is a button called attack and the use for the button is when it clicked the money value in the another script which called AmericaFire decreases by 60000 and this is the Code for the second script in the second scene:
void Start () {
GameObject MainCamera = GameObject.Find("MainCamera");
America America = MainCamera.GetComponent<America>();
America.AmericaFire -= 60000;
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
}
and when i press the button it do nothing and get an error in the console says:
NullReferenceException: object Reference not set to an instance of an object
what should i do to make it all works and what is the problem here
Note:I tried to make the DontDestroyOnLoad thing and it didnt work
Thank You previously
Your answer
Follow this Question
Related Questions
Access Variables From 1st Script to 2nd Script? (C#) 1 Answer
Why can't I access a variable from a different script? 0 Answers
get all variables of a type from an object's components 0 Answers
How can I access to child objects variables from other C# script ? 0 Answers
Unity 2D: Accessing variables from another script(Problem) 0 Answers