- Home /
How to access float from other script?
Why is my CurrentPetDamage = 0 in my Debug.Log when in inspector its showing something different? Any ideas? When I put some number in my DealPetDamage as a float it works, but when I use a float from other script it doesnt.
First snippet of script(On a button in shop):
public void PurchasedItem()
{
if (goldmanager.gold >= cost)
{
SpawnPet();
goldmanager.gold -= cost;
count += 1;
cost = Mathf.Round (baseCost * Mathf.Pow(1.25f, count));
DamagePerSec.Instance.PetDPS += PetDPSUpgrade;
CurrentPetDamage += PetDPSUpgrade;
Second snippet of script(EnemyHealth):
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag=="Pet")
{
Debug.Log("Pet attack for: " + PetManager.Instance.CurrentPetDamage);
DealPetDamage(PetManager.Instance.CurrentPetDamage);
}
else
{
DealProjectileDamage(Shooting.Instance.clickPower);
}
void DealPetDamage(float damageValue)
{
CurrentHealth -= PetManager.Instance.CurrentPetDamage;
healthbar.value = CalculateHealth();
if(CurrentHealth <= 0)
{
Die();
}
I don't see enough code to be of much help. Generally using a reference to an instance of another class isn't an issue for public values, but I can't see where Pet$$anonymous$$anager.Instance is getting it's value. Usually when you have mysteries like you describe it is the reference to the instance is pointing to the wrong instance.
Answer by 3kWikiGames · Jan 02, 2019 at 06:50 PM
You'll need to make sure that the value is a public static float variable to be accessed in another script. Then by writing the name of the script (period) and then the variable name it will be accessible. Ex: Onabuttoninshop.CurrentPetDamage += 2; (this would be in your enemyhealth script.
He already did that. He put the variable inside of a singleton. Didn’t you read his code?
Your answer
Follow this Question
Related Questions
OnDestroy collision detect 4 Answers
Problem about collisions between the cubes. 2 Answers
How to get the points of 2 collided objects 0 Answers
How do I “freeze” an object after a collision? 3 Answers
Camera Collision Not Working 0 Answers