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 Paffedd · Mar 17, 2015 at 05:56 AM · untiy3d

Decrease Stat function not working

Hello,

I would like to know why my function isn't working properly even though I am doing it correctly well!

 From PlayerStats class:
 
     public void DecreaseStat(float value, float value1)
     {
         value -= value1;
     }
 
 From PlayerController class:
 
     private void Shoot()
     {
         if(Input.GetKeyDown(KeyCode.F))
         {
             if(PlayerStats.Instance.HasEnergy(_playerProjectileEnergyCost))
             {
                 /*_playerEnergy -= 25;
                 _playerEnergy = Mathf.Clamp(_playerEnergy, 0.0f, _playerMaxEnergy);*/
 
                 PlayerStats.Instance.DecreaseStat(_playerEnergy,_playerProjectileEnergyCost);
                 
                 UpdateEnergyUI();
                 
                 GameObject projectile = (GameObject) Instantiate(_projectile, transform.position, Quaternion.identity);
                 //projectile.transform.SetParent(transform, false);
             }
         }
     }

It isn't decreasing my energy at all, but if I have my DecreaseStat function on the same class and I call it normally. It actually works. Is there any explanation for it ? I would love to know, I even wondered if it could be my Singleton.

Thank you very much for the attention.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Mar 17, 2015 at 06:08 AM

This method:

 public void DecreaseStat(float value, float value1)
 {
     value -= value1;
 }

Is pointless as it does nothing. All method parameters are passed by value (except "ref" or "out" parameters). That means the value is copied onto the methods local stack when it's executed. Since you don't return the changed local value, it won't change the value you originally passed to the method.

It's perfectly find to call your method like this:

 DecreaseStat(20, 2)

Inside your method the local "value" variable (which got the copied value "20" from the parameter) is indeed reduced by "2", but once the method finishes the local variable is gone.

May i ask where the "_playerEnergy" variable is actually declared? From your class naming i would expect to find this variable insice the PlayerStats class, but from your code snippet above it looks like you declared it inside of your PlayerController class.

All in all this piece of code is very confusing. A bit more background might be helpful.

Comment
Add comment · Show 6 · 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 Paffedd · Mar 17, 2015 at 07:31 AM 0
Share

I am sorry, here are both scripts, maybe you can understand better! Also thanks for the answers, it gave me some background.

PlayerStats: http://pastebin.com/A4aWSkRw PlayerController: http://pastebin.com/hhmPjirG

Thanks for the attention and if you have any tips how to improve my scripts, I also appreciate it!

Edit: I was gonna pass them by ref but after some research on ref, I would rather return the value!

avatar image Priyanshu · Mar 18, 2015 at 07:32 AM 0
Share

@Paffedd On the Pasebin site it shows "This paste has been removed!".

avatar image Priyanshu · Mar 18, 2015 at 07:35 AM 0
Share

If you want to return the value -

 public void DecreaseStat(float value, float value1)
 {
         return value -= value1;
 }


Utilizing Decrease stat method -

 float updatedEnergy = PlayerStats.Instance.DecreaseStat(_playerEnergy,_playerProjectileEnergyCost);
         
 UpdateEnergyUI(updatedEnergy );

avatar image Paffedd · Mar 18, 2015 at 09:41 AM 0
Share

Thank you and sorry, I will repost so you can have a look! Also you forgot to change void to float. But yes I changed them to return ins$$anonymous$$d of passing with the ref keyword.

PlayerController: http://pastebin.com/XWYGp6yv PlayerStats: http://pastebin.com/FhFb1QU4

Thanks so much for the attention and please if you take a look at my code and you think there is better ways for my code approachs, I would love to know!

avatar image Priyanshu · Mar 18, 2015 at 09:46 AM 0
Share

Did it work?

I am no good at coding myself :P

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Date File was edited? 0 Answers

Unity Build Error: Missing 'package' key attribute on element package at [:arcore_client:] AndroidManifest.xml:30:9-54 2 Answers

go to definition failed either because the caret is already at the definition or because an explicit definition could not be found 0 Answers

Unity UI Builder Toggle can't change checkmark size 1 Answer

Angle between vectors - calculation 2 Answers


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