In my game, there is a profit count which is controlled by the addMoney variable in my money script ex. profitcount = addMoney.
When I add the player pref regarding my addMoney Variable it defaults the profitCount to 0 when it should in fact be 1. This is my first game, so it could be be very easily a small thing that I have misunderstood or overlooked.
moneyCount
public class moneyCount : MonoBehaviour
{
float timeTillAdd = 1;
public int addMoney = 1;
public int money;
public Text txt;
// Start is called before the first frame update
void Start()
{
money = PlayerPrefs.GetInt("Money");
addMoney = PlayerPrefs.GetInt("addmoney");
}
// Update is called once per frame
void Update()
{
PlayerPrefs.SetInt("addmoney", addMoney);
if (Time.time >= timeTillAdd)
{
money += addMoney;
timeTillAdd++;
}
txt.text = money.ToString();
PlayerPrefs.SetInt("Money", money);
}
}
profit count
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class profitCount : MonoBehaviour
{
public int profitAmount;
public GameObject moneyManagerObj;
moneyCount mc;
public Text txt;
// Start is called before the first frame update
void Start()
{
mc = moneyManagerObj.GetComponent<moneyCount>();
// profitAmount = PlayerPrefs.GetInt("profitamount");
}
// Update is called once per frame
void Update()
{
profitAmount = mc.addMoney;
txt.text = profitAmount.ToString();
// PlayerPrefs.SetInt("profitamount", profitAmount);
}
}
shopManager
public class ShopManager : MonoBehaviour
{
public int item1_cost = 50;
public int item1_upgrade = 5;
public int item1_tier = 1;
public int item2_cost = 50;
public int item2_upgrade = 5;
public GameObject moneyManagerObj;
moneyCount mc;
public Text txt;
public Text item1;
public Text item1_text;
// Start is called before the first frame update
void Start()
{
mc = moneyManagerObj.GetComponent<moneyCount>();
item1_cost = PlayerPrefs.GetInt("Item1_cost");
//item1_upgrade = PlayerPrefs.GetInt("Item1_upgrade");
item1_tier = PlayerPrefs.GetInt("Item1_tier");
}
// Update is called once per frame
void Update()
{
item1.text = item1_tier.ToString();
PlayerPrefs.SetInt("Item1_cost", item1_cost);
// PlayerPrefs.SetInt("Item1_upgrade", item1_upgrade);
PlayerPrefs.SetInt("Item1_tier", item1_tier);
if (item1_tier > 0)
{
item1_text.text = ("Upgrade");
}
}
public void on_click()
{
{
if (mc.money >= item1_cost)
{
mc.money -= item1_cost;
mc.addMoney += item1_upgrade;
item1_tier += 1;
item1.text = item1_tier.ToString();
item1_cost += 50 * item1_tier;
}
}
}
}
Two things:
Don't use PlayerPrefs to store save data. It isn't meant for that. Its meant for saving player preferences such as volume, full screen mode, or input type. The file used by PlayerPrefs is plain text and can't support complex data types.
If no save data exists, the values read out are zero (at least from PlayerPrefs). You need to account for this and currently you are not. When you move to a different method of saving, you'll get other errors, like null pointers or file not founds. You have to determine if a save exists and only if it does, should you read from it.
Ok so you were assigning default value to addMoney during initialization i.e public int addMoney = 1;
But then in your start you were again assigning it a value that has not been saved yet addMoney = PlayerPrefs.GetInt("addmoney");
If you want to create a record do it like this PlayerPrefs.SetInt("addmoney",addmoney);
Related
For some reason I need to start the scene with the menu open, then close it, then grab the coin and go to the shop menu in order for the shop ui to update my moneyAmount. If i start the scene with the shop menu closed and pick up the coin then go to my shop menu it doesnt update. And when i buy my helmet it says reference not set to an object even though all im doing is getting my player health component and adding 50 to it so why do i need to reference any kind of object? here are the scripts with my GameControl script first.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour
{
public Text moneyText;
public static int moneyAmount;
int isHelmetSold;
int isBeltSold;
int isPantsSold;
int isShirtSold;
int isBootsSold;
// Start is called before the first frame update
void Start()
{
moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
isHelmetSold = PlayerPrefs.GetInt("IsHelmetSold");
isBeltSold = PlayerPrefs.GetInt("IsBeltSold");
isPantsSold = PlayerPrefs.GetInt("IsPantsSold");
isShirtSold = PlayerPrefs.GetInt("IsShirtSold");
isBootsSold = PlayerPrefs.GetInt("IsBootsSold");
}
// Update is called once per frame
void Update()
{
moneyText.text = moneyAmount.ToString();
}
Here is my PageOneShop script which makes my item buyable when amount is reached (for the time being ive only finished my helmet not the rest so if you can ignore all of the public texts and buttons as i know i havent added them yet)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PageOneShop : MonoBehaviour
{
public static int moneyAmount;
int isHelmetSold;
int isBeltSold;
int isPantsSold;
int isShirtSold;
int isBootsSold;
public Text moneyText;
public Text helmetPrice;
public Text beltPrice;
public Text pantsPrice;
public Text shirtPrice;
public Text bootsPrice;
public Button buyHelmetButton;
public Button buyBeltButton;
public Button buyPantsButton;
public Button buyShirtButton;
public Button buyBootsButton;
void Start()
{
moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
isHelmetSold = PlayerPrefs.GetInt("IsHelmetSold");
isBeltSold = PlayerPrefs.GetInt("IsBeltSold");
isPantsSold = PlayerPrefs.GetInt("IsPantsSold");
isShirtSold = PlayerPrefs.GetInt("IsShirtSold");
isBootsSold = PlayerPrefs.GetInt("IsBootsSold");
}
// Update is called once per frame
void FixedUpdate()
{
moneyText.text = moneyAmount.ToString();
isHelmetSold = PlayerPrefs.GetInt("IsHelmetSold");
if (moneyAmount >= 150 && isHelmetSold == 0)
buyHelmetButton.interactable = true;
else
buyHelmetButton.interactable = false;
isBeltSold = PlayerPrefs.GetInt("IsBeltSold");
if (moneyAmount >= 120 && isBeltSold == 0)
buyBeltButton.interactable = true;
else
buyBeltButton.interactable = false;
isPantsSold = PlayerPrefs.GetInt("IsPantsSold");
if (moneyAmount >= 100 && isPantsSold == 0)
buyPantsButton.interactable = true;
else
buyPantsButton.interactable = false;
isShirtSold = PlayerPrefs.GetInt("IsShirtSold");
if (moneyAmount >= 100 && isShirtSold == 0)
buyShirtButton.interactable = true;
else
buyShirtButton.interactable = false;
isBootsSold = PlayerPrefs.GetInt("IsBootsSold");
if (moneyAmount >= 80 && isBootsSold == 0)
buyBootsButton.interactable = true;
else
buyBootsButton.interactable = false;
}
public void buyHelmet()
{
moneyAmount -= 150;
GetComponent<PlayerHealth>().maxHealth += 50;
PlayerPrefs.SetInt("IsHelmetSold", 1);
helmetPrice.text = "Sold!";
buyHelmetButton.gameObject.SetActive(false);
}
public void buyBelt()
{
moneyAmount -= 120;
GetComponent<PlayerHealth>().maxHealth += 50;
PlayerPrefs.SetInt("IsBeltSold", 1);
helmetPrice.text = "Sold!";
buyBeltButton.gameObject.SetActive(false);
}
public void buyShirt()
{
moneyAmount -= 100;
GetComponent<PlayerHealth>().maxHealth += 50;
PlayerPrefs.SetInt("IsShirtSold", 1);
helmetPrice.text = "Sold!";
buyShirtButton.gameObject.SetActive(false);
}
public void buyPants()
{
moneyAmount -= 100;
GetComponent<PlayerHealth>().maxHealth += 50;
PlayerPrefs.SetInt("IsPantsSold", 1);
helmetPrice.text = "Sold!";
buyPantsButton.gameObject.SetActive(false);
}
public void buyBoots()
{
moneyAmount -= 80;
GetComponent<PlayerHealth>().maxHealth += 50;
PlayerPrefs.SetInt("IsBootsSold", 1);
helmetPrice.text = "Sold!";
buyBootsButton.gameObject.SetActive(false);
}
And here is my coin script on my coins that i pick up.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
void OnTriggerEnter2D (Collider2D col)
{
PageOneShop.moneyAmount += 200;
GameControl.moneyAmount += 200;
Destroy(gameObject);
}
It's important to figure out the pieces involved. Starting from the minimum amount of code, I think this is a good start:
"GameControl"
using UnityEngine;
using UnityEngine.UI;
public class GameControl : MonoBehaviour
{
public Text moneyText;
public static int moneyAmount;
// Start is called before the first frame update
// Start is only called while this is active and enabled
void Start()
{
moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
}
void Update()
{
moneyText.text = moneyAmount.ToString();
}
}
"PageOneShop"
using UnityEngine;
using UnityEngine.UI;
public class PageOneShop : MonoBehaviour
{
public static int moneyAmount;
public Text moneyAmountText;
public Text helmetPrice;
// Start is called before the first frame update
// Start is only called while this is active and enabled
void Start()
{
moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
}
// Update is called once per frame
void Update()
{
moneyAmountText.text = moneyAmount.ToString();
}
public void buyHelmet()
{
moneyAmount -= 150;
helmetPrice.text = "Sold!";
}
}
"Coin"
using UnityEngine;
public class Coin : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D col)
{
PageOneShop.moneyAmount += 200;
GameControl.moneyAmount += 200;
Destroy(gameObject);
}
}
Everything in these files is used, and we can replicate our issue. Before this point, my previous answer was correct and you weren't updating the moneyAmount instance inside PageOneShop. Now there is a new issue and it's more subtle, because it's strictly speaking not in your code.
Start is a Unity called method, and is called "before the first frame update". Start is also only called if the component is enabled and the object it's attached to is active. If the object it's attached to is de-activated (like I'm guessing your menu is when you load your scene) Start will be called the first time you activate the object (open the menu). Since nowhere are you updating the "MoneyAmount" in PlayerPrefs, that value is still 0. If you pick up the coin, then open the menu for the first time, the value would be 200 but instead is re-set to 0 when this runs:
void Start()
{
moneyAmount = PlayerPrefs.GetInt("MoneyAmount");
}
Probably the quickest way to fix this is to add this line to coin:
public class Coin : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D col)
{
PageOneShop.moneyAmount += 200;
GameControl.moneyAmount += 200;
// Set the PlayerPrefs value
PlayerPrefs.SetInt("MoneyAmount", GameControl.moneyAmount);
Destroy(gameObject);
}
}
At this point even if the menu opens after the coin is picked up, the correct value is retrieved from PlayerPrefs so nothing is overwritten.
Potentially a new problem has been introduced, which is even in the editor during testing, this write to PlayerPrefs is occurring so money may accumulate as multiple play sessions are run.
There are a few other issues as well. As #derHugo accurately points out you should stop polling for changes in update to an event based approach, and the use of two static variables which are kept in sync like this is at best wasteful, and at worst can cause some real serious headaches.
Let's start by addressing the double static variable use. You could replace all the references of one with the other, but I propose introducing a third class to handle the player's "Wallet" where they keep their money. You could write something as simple as:
public static class Wallet
{
public static int MoneyAmount;
}
All the references to the other two static moneyAmount variables could be replaced with this Wallet.MoneyAmount.
The polling could now be replaced with events pretty quickly by adding an event to the Wallet class, and replacing the field with a property:
using System;
public static class Wallet
{
public static event Action OnMoneyAmountChanged;
private static int moneyAmount;
public static int MoneyAmount
{
get
{
return moneyAmount;
}
set
{
moneyAmount = value;
// maybe null check, or follow a "never null" approach
OnMoneyAmountChanged();
}
}
}
Instead of always checking moneyAmount you can now add some code like:
void Start()
{
// Listen for changes
Wallet.OnMoneyAmountChanged += handleMoneyAmountChanged;
}
private void OnDestroy()
{
// Remember to always remove your listeners when you're done with them
Wallet.OnMoneyAmountChanged -= handleMoneyAmountChanged;
}
// set the text to the value held in the wallet whenever the amount changes
void handleMoneyAmountChanged()
{
moneyText.text = Wallet.MoneyAmount.ToString();
}
Maybe you don't want the PlayerPrefs.SetInt call in Coin any more, and instead you could move it into the Wallet now. If you put it in a [Conditional] method you could also protect yourself from constantly overwriting PlayerPrefs with every play session in the editor.
using System;
using System.Diagnostics;
using UnityEngine;
public static class Wallet
{
public static event Action OnMoneyAmountChanged;
private static readonly string moneyAmountKey = "MoneyAmount";
private static int moneyAmount;
public static int MoneyAmount
{
get
{
return moneyAmount;
}
set
{
moneyAmount = value;
OnMoneyAmountChanged();
// this method call is conditional on a defined constant
saveMoneyAmount();
}
}
// This part will only run if ENABLE_PLAYERSPREFS is defined.
[Conditional("ENABLE_PLAYERPREFS")]
private static void saveMoneyAmount()
{
PlayerPrefs.SetInt(moneyAmountKey, moneyAmount);
}
}
You can type whatever you want into the scripting define symbols text box described in the "Platform custom #defines" section on this page. If you define "ENABLE_PLAYERPREFS" that method will call.
You probably now want to initialize your wallet as well, and to keep things symmetrical you could "close" it as well. This would let you cut down on a lot of reads and writes to player prefs.
public static void Open()
{
// this is another way to interact with platform define constants
#if ENABLE_PLAYERPREFS
moneyAmount = PlayerPrefs.GetInt(moneyAmountKey);
#else
moneyAmount = 0;
#endif
}
public static void Close()
{
// maybe you want to do other stuff in here
saveMoneyAmount();
}
If you called Open at the start of your application, and Close at the end you could just use the variables in memory the rest of the time and remove the saveMoneyAmount call from the MoneyAmount setter.
As for this second question you added later than my original answer, "And when i buy my helmet it says reference not set to an object even though all im doing is getting my player health component and adding 50 to it so why do i need to reference any kind of object?". You didn't post the line number, but I can guess that it originates on any/all the calls that look like this GetComponent<PlayerHealth>().maxHealth += 50; since those are all in methods on PageOneShop so unless you've added your PlayerHealth component to your menu object that GetComponent will be null, at which point you can refer to What is a NullReferenceException, and how do I fix it?
hi I am new to unity and C# and I am making my first game. In it I have a Scriptable object named "sss".It contains values like coins,speed,jump power. It works fine in the editor but after I build it it wont work.what I mean by it wont work is after the player dies in the game depending on how long they live they will get a certain amount of coins when they go to the store they will see those coins displayed as a UI and they can spend them on things like speed and jump boost. this works in the editor but not in a build. Dose anybody know why this is? here is the code I have in my Scriptable object
using UnityEngine;
[CreateAssetMenu(fileName = "data",menuName = "sss",order = 1)]
public class sss : ScriptableObject
{
public float coins = 0f;
public float speed = 10f;
public float jump = -9.81f;
public bool set = true;
public int face = 1;
}
here is the code I use to display the coins float
coinstext.text = sss.coins.ToString();
and here is the whole store-manager script used to buy stuff
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Security;
using UnityEngine;
using UnityEngine.UI;
public class storemanager : MonoBehaviour
{
public sss sss;
public Text coinstext;
public Slider sliders;
public Slider sliderj;
public int maxcap = 30;
public int maxcapj = 500;
public void Start()
{
coinstext.text = sss.coins.ToString();
}
public void speedbuy()
{
if (sss.coins >= 5 && sss.speed < maxcap)
{
sss.speed += 1;
sss.buyspeed = true;
sss.coins -= 5;
sliders.value += 1;
coinstext.text = sss.coins.ToString();
}
}
public void jumpbuy()
{
if (sss.coins >= 7 && sss.jump < maxcapj)
{
sss.jump += 10;
sss.buyjump = true;
sss.coins -= 7;
sliderj.value += 10;
coinstext.text = sss.coins.ToString();
}
}
}
While changes in ScriptableObject within the UnityEditor are persistent, they are not persistent in a build!
When you use the Editor, you can save data to ScriptableObjects while editing and at run time because ScriptableObjects use the Editor namespace and Editor scripting. In a deployed build, however, you can’t use ScriptableObjects to save data, but you can use the saved data from the ScriptableObject Assets that you set up during development.
So after a build the data in the asset is kind of baked and you can change it at runtime but when restarting the app you will always re-load the data of the build.
You would need to store your data instead in a local file stored on the device itself e.g. using Json like
using UnityEngine;
using System.IO;
...
public class sss : ScriptableObject
{
public float coins = 0f;
public float speed = 10f;
public float jump = -9.81f;
public bool set = true;
public int face = 1;
private const string FILENAME = "sss.dat";
public void SaveToFile()
{
var filePath = Path.Combine(Application.persistentDataPath, FILENAME);
if(!File.Exists(filePath))
{
File.Create(filePath);
}
var json = JsonUtility.ToJson(this);
File.WriteAllText(filePath, json);
}
public void LoadDataFromFile()
{
var filePath = Path.Combine(Application.persistentDataPath, FILENAME);
if(!File.Exists(filePath))
{
Debug.LogWarning($"File \"{filePath}\" not found!", this);
return;
}
var json = File.ReadAllText(filePath);
JsonUtility.FromJsonOverwrite(json, this);
}
}
And then in your manager script call the load and save methods e.g.
public class storemanager : MonoBehaviour
{
public sss sss;
public Text coinstext;
public Slider sliders;
public Slider sliderj;
public int maxcap = 30;
public int maxcapj = 500;
public void Start()
{
sss.LoadFromFile();
coinstext.text = sss.coins.ToString();
}
// Called when the application is going to quit
private void OnApplicationQuit()
{
sss.SaveToFile();
}
Note: Typed on smartphone but I hope the idea gets clear
I am making a small platform game and in that, I made a scene where you can change the colour of your player. I made 3 buttons that change the colour when you click on them. I also made two code files, but they are not working. I see no errors in the console either.
P.S the colour changing code and buttons are on a different scene than the game object.
This is the code for the buttons that change the colour:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColourManager : MonoBehaviour
{
public static int colour;
public void DefaultBlue()
{
colour = 0;
}
public void Green()
{
colour = 1;
}
public void Red()
{
colour = 2;
}
}
This is the code on the game object itself:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColourTarget : MonoBehaviour
{
void Start()
{
rend = GetComponent<Renderer>();
rend.sharedMaterial = materials[0];
rend.enabled = true;
}
private Renderer rend;
public int lcolour = ColourManager.colour;
public Material[] materials;
private void Update()
{
if (lcolour == 0)
{
rend.sharedMaterial = materials[0];
}
if (lcolour == 1)
{
rend.sharedMaterial = materials[1];
}
if (lcolour == 2)
{
rend.sharedMaterial = materials[2];
}
}
}
Note that changing sharedMaterial might not be what you want to do here.
If you want to modify the material of a renderer use material instead.
so rather use material. Especially once e.g. a color was changed and you are dealing with instanced materials, afterwards changing the shared material has no effect at all.
Then do not do this in Update! It is very redundant and inefficient setting this every frame!
Finally note that this
public int lcolour = ColourManager.colour;
is only assigned ONCE the moment this object is initialized and then never changed anymore ... int is a VALUE type, not a reference!
I would rather use and event and make your target listen to any changes.
So your code might look like
public class ColourManager : MonoBehaviour
{
public static int colour;
// we will invoke this event everytime the color index is changed
// and directly pass the according new index in
public static event Action<int> OnColourIndexChanged;
public void DefaultBlue()
{
colour = 0;
// The ? is a null check and only
// calls Invoke if there is at least one listener to this event
OnColourIndexChanged?.Invoke(colour);
}
public void Green()
{
colour = 1;
OnColourIndexChanged?.Invoke(colour);
}
public void Red()
{
colour = 2;
OnColourIndexChanged?.Invoke(colour);
}
}
and then
public class ColourTarget : MonoBehaviour
{
[SerializeField] private Renderer _renderer;
public Material[] materials;
private void Awake()
{
if(!_renderer) _renderer = GetComponent<Renderer>();
_renderer.enabled = true;
// Add a callback to the event
// Removing it first is save also if it wasn't added so far
// This just makes sure it is always added only exactly once
ColourManager.OnColourIndexChanged -= UpdateMaterial;
ColourManager.OnColourIndexChanged += UpdateMaterial;
// do the first update now with the current state
UpdateMaterial(ColourManager.colour);
}
// Now this is called only when the value is changed in the manager
// script and once at the beginning with the initial state
private void UpdateMaterial(int index)
{
// check for validity
if(index < 0 || index >= materials.Length) return;
_renderer.material = materials[index];
}
private void OnDestroy()
{
// Always make sure to clean up listeners once not needed anymore
// otherwise you get NullReferencExceptions
ColourManager.OnColourIndexChanged -= UpdateMaterial;
}
}
I have 2 scenes each one have own script simply I want to get a value from script 1 and use it in script 2 which is loaded in the second scenes which happen is it always load the default value for this variable ex; if I call string variable which should have value (sas) it appears in the second script with null if integer it will be 0 although if I print it in the first script it will have the right value which "sas" .
look to method goforward () which update the variable x
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Trainbehavior : MonoBehaviour
{
public float force = (float)0.1;
private Rigidbody sph;
public GameObject button_goforward;
public GameObject button_goright;
public GameObject button_results;
public GameObject button_nextlevel;
public Text Results;
public String x ;
//public int g;
//public gamepbject panel ;
//panel.gameobject.setactive(true);
private void Start()
{
sph = GetComponent<Rigidbody>();
button_goforward.SetActive(false);
button_goright.SetActive(false);
button_results.SetActive(false);
button_nextlevel.SetActive(false);
}
private void OnTriggerEnter(Collider other)
{
if (other.name == "switchtrigger")
{
button_goforward.SetActive(true);
button_goright.SetActive(true);
button_results.SetActive(true);
Time.timeScale = 0;
}
}
public void goforward()
{
setx("sas"); // if a called print x it will be sas
// Results.text = "dzf";
}
public String getx()
{
return x;
}
public void setx(String x)
{
this.x = x;
}
second script
in the update iam trying to get the value x by
finalresults.text = trains.getx();
public class cases : Trainbehavior
{
Trainbehavior trains;
public Text finalresults;
public string cx;
// Start is called before the first frame update
void Start()
{
trains = GetComponent<Trainbehavior>();
/*var trainscript = gameObject.GetComponent<Trainbehavior>();
if (trainscript)
{
finalresults.text = Results.GetComponent<UnityEngine.UI.Text>().text;
}*/
}
// Update is called once per frame
void Update()
{
// finalresults.text = trains.x;
finalresults.text = trains.getx(); // if i called print(x) it will be null !
/*
var trainscript = gameObject.GetComponent<Trainbehavior>();
if (trainscript)
{
// GameObject x = trainscript.GameObject.Find("test");
finalresults.text = cx;
print(cx);
}*/
}
}
If I understand your question you just want to call your getx function and return the value you set to x in the first script. However Unless you call that goForward function your not setting x and I don't see where your doing that. It looks like your saying when you do call goforward you get what you want which makes sense. you just need to call that goforward function in your second script to set x to a value before you call getx.
Your not inheriting x when you do this your just setting a public variable which is fine. you can either set the value to your public variable x in that function or anywhere in that class.
public static int pscore = 0;
void Start()
{
InvokeRepeating("AddToMoney",1, 1);
}
void AddToMoney ()
{
pscore++;
}
I have this code to add a point to integer pscore every second. But right now it adds 2 points instead of one. Is it because it's static? And if so, how can I fix it? Or how can I transfer one variable from script to another script without using static?
Note: I couldn't get answer in the unity3d forums, that's why I'm asking here.
add a point to integer pscore every second
bool keepIncrementing = false;
public static int pscore = 0;
void Start()
{
StartCoroutine(IncementEachSecond());
}
IEnumerator IncementEachSecond()
{
keepIncrementing = true;
while (keepIncrementing)
{
pscore++;
yield return new WaitForSeconds(1);
}
}
void stopIncrementing()
{
keepIncrementing = false;
}
how can I transfer one variable from script to another script without
using static
public class ScriptA : MonoBehaviour{
public int pscore = 0;
void Start()
{
}
}
Access variable pscore in ScriptA from ScriptB.
public class ScriptB : MonoBehaviour{
ScriptA scriptInstance = null;
void Start()
{
GameObject tempObj = GameObject.Find("NameOfGameObjectScriptAIsAttachedTo");
scriptInstance = tempObj.GetComponent<ScriptA>();
scriptInstance.pscore = 5;
}
}