- Home /
ManaBar plus pause
I'm using this script so that when I pause, I use Mana. But if I'm out of mana, I can't pause anymore. And script error still lets me pause. Iv'e tried to stop it, but I get this error : Assets/My Scripts/Freeze.cs(30,27): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer.
Anyways, Her's the script:
using UnityEngine; using System.Collections;
public class Freeze : MonoBehaviour {
int p = 0;
private GameObject target;
void Start() {
target = GameObject.FindGameObjectWithTag("player");
} void Update () {
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;
}
if((PlayerManaBar)target.GetComponent("PlayerManaBar") = 100)
{
Time.timeScale=1;
}
}
}
}
I'm bummed...
You get an error on a line which doesn't appear to be in your post, is that right? Edit - o never$$anonymous$$d that way you posted this broke it up such that it didn't start line #1. This line looks suspicious: if((Player$$anonymous$$anaBar)target.GetComponent("Player$$anonymous$$anaBar") = 100)
Did you mean if(eh.$$anonymous$$anaValue = 100)?
Answer by TrickyHandz · Aug 29, 2013 at 03:14 AM
The offending error is due to this conditional:
if ((PlayerManaBar)target.GetComponent("PlayerManaBar") = 100)
Remember that a single equal sign is an assignment and equality checks are done with a double equals sign. Additionally, your class "PlayerManaBar" can never be equal to a value of 100 so I'm guessing you are trying to access a property within the class itself. As was mentioned by @getyour411 in his comments. I would highly recommend using the typed version of GetComponent rather than the version with a string parameter. Then you can access properties within your conditional like this:
if(target.GetComponent<PlayerManaBar>.CurrentMana == 100)
Finally, you are doing redundant casting of the PlayerManaBar class and you shouldn't be. You need not cast the result of GetComponent() since it will only return the class you are looking for. All of these do the same thing:
// Redundant casting. The GetComponent
// function will return the instance of
// the PlayerManaBar class, but you are
// explicitly casting it as a PlayerManaBar
// which is unnecessary
PlayerManaBar eh = (PlayerManaBar)target.GetComponent("PlayerManaBar");
// Assignment using string parameter
// overload of GetComponent function
PlayerManaBar eh = target.GetComponent("PlayerManaBar");
// Assignment using type parameter
// overload of GetComponent function
// This is the best way in my opinion
PlayerManaBar eh = target.GetComponent<PlayerManaBar>();
Hope that helps you sort this out.
I get this error called: Assets/$$anonymous$$y Scripts/Freeze.cs(31,31): error CS0119: Expression denotes a method group', where a
variable', value' or
type' was expected
The script looks like this now:
using UnityEngine; using System.Collections;
public class Freeze : $$anonymous$$onoBehaviour {
int p = 0;
private GameObject target;
void Start() {
target = GameObject.FindGameObjectWithTag("player");
} void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P))
{
if (p == 0)
{
Time.timeScale=0;
Screen.showCursor = true;
Screen.lockCursor = false;
p = 1;
Player$$anonymous$$anaBar eh = (Player$$anonymous$$anaBar)target.GetComponent("Player$$anonymous$$anaBar");
eh.AddjustCurrent$$anonymous$$ana(-100);
GUI.Button(new Rect(130, 130, 100, 70), "Rewinding!");
}
else
{
Time.timeScale=1.0f;
p=0;
if(target.GetComponent<Player$$anonymous$$anaBar>.Current$$anonymous$$ana == 0)
{
Time.timeScale=1.0f;
}
}
}
} }
Your answer
![](https://koobas.hobune.stream/wayback/20220613113157im_/https://answers.unity.com/themes/thub/images/avi.jpg)