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), "");	
	
}
}

2 Answers

2

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 (curMana == 0) { p=0; }

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

Reset it via code in the OnEnable method. Set it directly to default.