Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by spiralfire11 · Aug 29, 2013 at 12:07 AM · c#pausinghealth

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...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Aug 29, 2013 at 12:51 AM 0
Share

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)?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spiralfire11 · Aug 29, 2013 at 08:31 PM 0
Share

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

avatar image spiralfire11 · Aug 29, 2013 at 08:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Health Script/Targeting system c# 2 Answers

My player HP are get decreased from several GameObjects that has is Trigger on 0 Answers

Floating Healthbar Appears when facing opposite direction of enemy 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges