0

I'm pretty new to C# and Unity, so sorry if my question is too simple. I'm trying to create an easy Upgrade System made from text and button for each stat. I made the Text script to show my "Attack Damage" stat, which worked. Now, I want to create a script Button so once that I click it my stat will go from (ex. 10 to 11) or anything. So my question is: how can I access variables from another script so that i can use them to be incremented by clicking the button? I'll attach both the scripts, please try to explain as simple as you can, so that a newbie can understand. Thanks!

Attack Damage Text Script ( Keep in mind that in Player class the heroDamage is set to 10f)

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class AttackDamage : MonoBehaviour { public static float attackDamage = Player.heroDamage; public Text attackDamageText; // Start is called before the first frame update void Start() { attackDamageText.text = ADButton.attack.ToString(); //here it was attackDamage.ToString() at first but i wanted to see if it works like that. } // Update is called once per frame void Update() { } } 

Attack Damage Button Script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ADButton : MonoBehaviour { public Button attackDamageButton; public static float attack; // Start is called before the first frame update void Start() { attackDamageButton.onClick.AddListener(Update); } // Update is called once per frame void Update() { attack = AttackDamage.attackDamage; if (Input.GetMouseButtonDown(0)) attack++; } } 

I guess my second code is wrong, but I don't know how can I modify it.

0

1 Answer 1

0

https://answers.unity.com/questions/1336162/how-to-reference-a-class-in-another-script-properl.html

This shows various possible methods. But the simpler is reference by object.

like

public class AttackDamage : MonoBehaviour { // Start is called before the first frame update void Start() { } public void Method1() { // Do Something like Attack++; } } public class ADButton : MonoBehaviour { public AttackDamage obj; // Start is called before the first frame update void Start() { obj.Method1(); } } 

Also in this case you need to assign the class AttackDamage in inspector on GameObject having ADButton script.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.