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
Related
I am a beginner developer and I am trying to create a timer that shows how many seconds have passed since the start of the game on Unity. I want to change the text component (I am using TextMeshPro to be specific) of the object with the script.
I have been searching on google but nothing has worked yet. I can't find a way to link the public Text to the text component, and I get an error because the text is null.
Well you should reference the text in your code and then change it with a coroutine
public class Timer : MonoBehaviour
{
[SerializeField]
private TMP_Text textComponent;
private int seconds = 0;
private IEnumerator TimeRoutine()
{
while(Application.isPlaying)
{
textComponent.text = seconds.ToString();
yield return new WaitForSeconds(1f);
seconds++;
}
}
private void Start()
{
StartCoroutine(nameof(TimeRoutine));
}
}
Code for Timer with Text mesh pro
using UnityEngine;
using TMPro;
public class Timerexample : MonoBehaviour
{
float cntdnw = 30.0f;
public TMP_Text disvar;
void Update()
{
if(cntdnw>0)
{
cntdnw -= Time.deltaTime;
}
double b = System.Math.Round (cntdnw, 2);
disvar.text = b.ToString ();
if(cntdnw < 0)
{
Debug.Log ("Completed");
}
}
}
Source: Unity timer
I am new with unity and C#, I have question about how I save current scrollrect position.
Example : I am scrolling the view , and move to another scene and then back to previous scene but the scroll shows the previous position before I moved the scene, not resetting the scroll to default.
Unfortunately, what you want to make is not available ready-made, you have to make it yourself
first use Recyclable-Scroll-Rect
When scrolling to the bottom of the scroll, you have to save the id you sent to DemoCall via PlayerPrefs, then when you go to another scene and back again to the selected scene, call the scroll info from the point it left off, which is the id you saved
EDIT
After adding the Recyclable-Scroll-Rect, you can use this code
using System.Collections.Generic;
using UnityEngine;
using PolyAndCode.UI;
using System.Collections;
public struct ContactTsnif
{
public string id;
}
public class Objx
{
public string id;
}
public class RecyclTsnif : MonoBehaviour, IRecyclableScrollRectDataSource
{
[SerializeField]
RecyclableScrollRect _recycHat;
public GameObject RecyScrHat;
[SerializeField]
public int _dataLenHat;
public int beginning;
private List<ContactTsnif> _contactList = new List<ContactTsnif>();
public List<string> id = new List<string>();
void Start()
{
beginning = PlayerPrefebs.GetInt("Start", 5)// start with 5
GetHat();
}
public void GetHat()
{
_dataLenHat = 0;
_recycHat.DataSource = this;
InitDataHat();
RecyScrHat.GetComponent<RecyclableScrollRect>().Initialize();
}
public void InitDataHat()
{
if (_contactList != null) _contactList.Clear();
for (int i = beginning; i < _dataLenHat;)
{
ContactTsnif obj = new ContactTsnif();
obj.id = id[i];
i++;
_contactList.Add(obj);
}
}
#region DATA-SOURCE
public int GetItemCount()
{
return _contactList.Count;
}
public void SetCell(ICell cell, int index)
{
var item1 = cell as DemoTsnif;
item1.ConfigureCellSor(_contactList[index], index);
}
#endregion
}
Demo
using UnityEngine;
using System;
using System.Collections;
public class DemoTsnif : MonoBehaviour, ICell
{
private ContactTsnif _ContactInfo;
private int _cellIndex;
public int id;
public void GetData()
{
}
public void ConfigureCellSor(ContactTsnif contactInfo,int cellIndex)
{
_cellIndex = cellIndex;
_ContactInfo = contactInfo;
id = contactInfo.id;
GetData();
}
}
Do you tried read / write normalizedPosition?
You basically need to do two things:
You need to attach this script to the GameObject which contains the ScrollRect in order to persist the position:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems; // Required when using event data
using UnityEngine.UI;
public class DragNotify : MonoBehaviour, IEndDragHandler // required interface when using the OnEndDrag method.
{
//Do this when the user stops dragging this UI Element.
public void OnEndDrag(PointerEventData data)
{
PlayerPrefs.SetFloat("scrollX", this.GetComponent<ScrollRect>().normalizedPosition.x);
}
}
You also need to apply the normalizedPosition once you initialized the ScrollRect and after you applied the desired content:
this.transform.Find("Scroll View").GetComponent<ScrollRect>().normalizedPosition = new Vector2(PlayerPrefs.GetFloat("scrollX"), 0F);
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?
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);
UPDATE:
The code from the answer didn't work but I had some progress, but I am still having issues. Updated.
This is my UpgradeUI script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
OnClick functions do work also with the Debug.Log. The code below is the Stats script which also updates my HealthUI.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class Stats
{
public static Stats st;
[SerializeField]
private BarScript bar;
public float maxVal;
public float currentVal;
public int playerDamage = 20;
public float CurrentVal
{
get
{
return currentVal;
}
set
{
this.currentVal = Mathf.Clamp(value, 0, maxVal);
bar.Value = currentVal;
}
}
public float MaxVal
{
get
{
return maxVal;
}
set
{
this.maxVal = value;
bar.MaxValue = value;
}
}
public void Initialize()
{
this.MaxVal = maxVal;
this.CurrentVal = currentVal;
}
}
I did progress a bit. I placed this code:
public void HpUp () { stats.maxVal += 20; stats.currentVal += 20; }
public void DmgUp() { stats.playerDamage += 10; }
into my PlayerManager and it connects with the Stats script I shared and my health does update. I removed the UpgradeMenuUI. Now the problem is that my damage does not update. Well actually it updates but the value doesn't reach where I need it to. I added a
public int playerDamage;
in my Stats script. In my Damage script, which calculates the damage for my Player and the enemy, I changed
aiManager.Enemy1Damage(playerDamage); to
aiManager.Enemy1Damage(stats.playerDamage);
and I call my playerDamage from Stats into my Damage script with these:
public Stats stats;
and
public void Awake()
{
stats = new Stats();
}
So the problem is that if I change the playerDamage in my Stats script from
this: public int playerDamage;
to
public int playerDamage = 20;
The value does work and my Player damage is indeed 20 when I hit the AI. However in the inspector my damage(the public int from Stats script) is 0 and if I press my upgrade damage button it goes up by 10 (that's what I set in my PlayerManager script:
public void DmgUp()
{
stats.playerDamage += 10;
}
), but that doesn't affect in any way the value 20. So it is just useless, like pumping air. I don't know how to make the public int playerDamage to update from the OnClick. If make it just playerDamage; without a number nothing updates and the damages is 0.
You have the Stats as a Singleton but are accessing a new instance of it on your button. Try changing:
public void HpUpgrade()
{
stats.maxVal = (int)(stats.Instance.maxVal += hpUp);
}
public void DmgUpgrade()
{
dmg.playerDamage = (int)(stats.Instance.maxVal += dmgUp);
}
I can't quite tell if you've confirmed that your OnClick event is triggering for the buttons. If they aren't, make sure that you have the EventSystem game object in your scene.