- Home /
Question by
spiralfire11 · Dec 09, 2013 at 09:05 PM ·
pausehealthbar
Manabar Pause Limit
I"m trying to get that the player can no longer pause when the ManaBar is empty. But I just can't seem to get a script in my mind for it... Here's the Pausing script:
using UnityEngine;
using System.Collections;
public class Freeze : MonoBehaviour {
public int p = 0;
private GameObject target;
public void Start() {
target = GameObject.FindGameObjectWithTag("player");
}
public void Update () {
AddjustCurrentfreeze(0);
}
public void AddjustCurrentfreeze(int adj) {
p += adj;
if (Input.GetKeyDown(KeyCode.P))
{
if (p == 0)
{
Time.timeScale=0;
Screen.showCursor = true;
Screen.lockCursor = false;
p = 1;
PlayerManaBar eh = (PlayerManaBar)target.GetComponent("PlayerManaBar");
eh.AddjustCurrentMana(-100);
}
else
{
Time.timeScale=1.0f;
p=0;
}
}
}
}
Here's the ManaBar script :
using UnityEngine;
using System.Collections;
public class PlayerManaBar : MonoBehaviour {
public GameObject target;
GUIStyle style = new GUIStyle();
Texture2D texture;
Color yeColor = Color.yellow;
Color cyanColor = Color.cyan;
private int curMana = 1000;
private int maxMana = 1000;
void Start()
{
texture = new Texture2D(1, 1);
texture.SetPixel(1, 1, cyanColor);
}
void Update()
{
AddjustCurrentMana(0);
}
public void AddjustCurrentMana(int adj) {
curMana += adj;
if(curMana < 0)
{
curMana = 0;
}
if(curMana > maxMana)
{
curMana = maxMana;
}
if(maxMana < 1)
{
maxMana = 1;
}
if (curMana <= 1000)
{
texture.SetPixel(1, 1, cyanColor);
}
}
public void OnGUI()
{
texture.Apply();
style.normal.background = texture;
GUI.Box(new Rect(10, 40, curMana, 20), new GUIContent(""), style);
GUI.Box(new Rect(10, 40, 1000, 20), "");
}
}
Comment
Best Answer
Answer by spiralfire11 · Dec 12, 2013 at 02:06 AM
Dont' worry I fixed myself:
using UnityEngine;
using System.Collections;
public class PlayerManaBar : MonoBehaviour {
public GameObject target;
GUIStyle style = new GUIStyle();
Texture2D texture;
Color yeColor = Color.yellow;
Color cyanColor = Color.cyan;
public int curMana = 1000;
private int maxMana = 1000;
void Start()
{
texture = new Texture2D(1, 1);
texture.SetPixel(1, 1, cyanColor);
}
void Update()
{
AddjustCurrentMana(0);
}
public void AddjustCurrentMana(int adj) {
curMana += adj;
if(curMana < 0)
{
curMana = 0;
}
if(curMana > maxMana)
{
curMana = maxMana;
}
if(maxMana < 1)
{
maxMana = 1;
}
if (curMana <= 1000)
{
texture.SetPixel(1, 1, cyanColor);
}
**if (curMana <= 0) {
Freeze eh = (Freeze)target.GetComponent("Freeze");
eh.AddjustCurrentfreeze(1);**
}
}
public void OnGUI()
{
texture.Apply();
style.normal.background = texture;
GUI.Box(new Rect(10, 40, curMana, 20), new GUIContent(""), style);
GUI.Box(new Rect(10, 40, 1000, 20), "");
}
}
Then I selected my target as my main character
Answer by foobuggy · Dec 09, 2013 at 11:00 PM
Just slightly modifying your current scripts - how about adding a public variable to PlayerManaBar like this:
public int mana{ get{ return curMana; }}
then in your Freeze script get the PlayerManaBar component before you check for the "P" keypress and just modify your "if" statement to be something like:
PlayerManaBar eh = (PlayerManaBar)target.GetComponent("PlayerManaBar");
if(p == 0 && eh.mana > 0){ ... do stuff ... }
its supposed to be if (cur$$anonymous$$ana == 0) { p=0; }