2

I'm new in unity someone can help me?

for example my health bar value is 100 if you want to upgrade it will cost about 10 coins and if I upgrade my health bar the value becomes 105 and the price of the health bar also increases. How can I do that? I searched a lot, but i didn't see that kind of upgrade. This is my health bar

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthManager : MonoBehaviour {

public int maxPlayerHealth;
public static int playerHealth;
//Text text;
public Slider healthBar;

public bool isDead;

public GameObject carExplosionEfect;


// Use this for initialization
void Start () {

    //text = GetComponent<Text> ();
    healthBar = GetComponent<Slider>();

    playerHealth = maxPlayerHealth;

    isDead = false;
}

// Update is called once per frame
void Update () {
    if (playerHealth <= 0 && !isDead) 
    {
        playerHealth = 0;

        isDead = true;

        Instantiate (carExplosionEfect, transform.position, transform.rotation);
    }

    if (playerHealth > maxPlayerHealth) {
        playerHealth = maxPlayerHealth;
    }

    healthBar.value = playerHealth;
}

public static void HurtPlayerOnContact(int damageToGive)
{
    playerHealth -= damageToGive;
}

public void FullHealth()
{
    playerHealth = maxPlayerHealth;
}
}
moonwalker7
  • 1,122
  • 3
  • 11
  • 29
P.Degs
  • 21
  • 2

2 Answers2

1

First of all a want to give you a simple advice,don't put together the UI code and player data control. Create a Upgrade method taking the amount of helth you want to increase,update variables maxPlayerHealth and playerHealth,beside update the slider max value too.Any other doubt ask again.

Jasiel
  • 121
  • 5
0

Make different scripts to keep the things sorted.

For coin/gold handling: MoneyManager.cs, whenever money is spent or gained, it goes through here.

For stats: StatsManager.cs, keeps track of current health and other stats you may have.

For purchasing upgrades: UpgradeShop.cs, can be accessed to purchase better stats, has methods that receive coins/gold and returns a stat bonus, for example

int UpgradeHealth(int price)
{
    return price*2; //assuming 1 coin buys 2 health.
}

You may then create references between the different scripts to make them work together.

This can be done by making fields public, or adding the [SerializeField] tag to a private field and then dragging the script in at the inspector.

It can also be done through more elaborate code.

For example, if you only ever want one money manager, you can make a static variable for MoneyManager where it has a MoneyManager, like this.

private static MoneyManager _Instance;

void Awake(){
_Instance = this;
}

And then place that code inside the MoneyManager, it may then be called from other scripts by saying.

MoneyManager._Instance.YourMethod();

So simply make the different scripts only handle their own assigned task, connect them and have them work together.

EDIT:

The Awake() method I use in my example is part of the Unity lifecycle.

Which you may read more about here.

https://docs.unity3d.com/Manual/ExecutionOrder.html

Doh09
  • 2,324
  • 1
  • 16
  • 31
  • i have shop, money, shield(powerups) script but for some reason my money was only show in my level 1 scene not in the shop scene and i cant buy the shield that i put in the shop – P.Degs Dec 10 '17 at 17:05
  • You need to show some code to allow me and others to have any chance of helping you. Preferably make a new post. – Doh09 Dec 10 '17 at 19:08
  • https://stackoverflow.com/questions/47747653/how-to-put-the-money-text-in-my-shop-scene here i post it i hope you can help me sir – P.Degs Dec 11 '17 at 06:45