Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dok101 · Jun 22, 2016 at 07:54 AM · variablefunctionfloatreturnargument

How to change the variables in my argument through a function?

Hello

I am currently trying to change a variable, which is in the argument of my function, and set the value of the variable in my argument to the value of the variable in the function. Apologies if I have not clearly articulated my question. Here is an example.

Here are 2 segments of my code :

     float SetDifficulty (float Difficulty)
         {
             Difficulty = (float)(Stage)-0.5f;
             Debug.Log("Difficulty : " + Difficulty);
             return Difficulty;
         }

and

         SetDifficulty(P1.Difficulty);
 

What I am trying to do is to set P1.Difficulty to what the result of Difficulty is in my function. In my console, Difficulty is 0.5 in the function SetDifficulty, however P1.Difficulty is always 0.

If anyone can assist, it would be a great help.

Thank you in advance

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SarperS · Jun 22, 2016 at 08:48 AM

You have 2 options. Either assign the returned value or use the "ref" keyword.

Option 1

 float SetDifficulty() {
     return (float)(Stage)-0.5f;
 }
 
 P1.Difficulty = SetDifficulty();

Option 2

 void SetDifficulty (ref float Difficulty) {
     Difficulty = (float)(Stage)-0.5f;        
 }
 
 SetDifficulty(ref P1.Difficulty);
Comment
Add comment · Show 4 · 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 Bunny83 · Jun 22, 2016 at 10:10 AM 0
Share

option 2 needs the "ref" key words as well when you call the method:

 SetDifficulty(ref P1.Difficulty);
avatar image SarperS Bunny83 · Jun 22, 2016 at 10:11 AM 0
Share

Ah, nice catch, thanks :)

avatar image Dok101 · Jun 22, 2016 at 12:11 PM 0
Share

Thanks @Bunny83 and @Sarper Soher. I would never have seen that on my own. That's a great help

avatar image Owen-Reynolds · Jun 23, 2016 at 06:43 AM 0
Share

This, and much more, is in any "Intro to C#" book. I've had people tell me the HeadsUp(?) series is decent.

avatar image
0

Answer by ETech-Studios · Jun 22, 2016 at 03:58 PM

Try this:

 P1.Difficulty = SetDifficulty(newValue);

Replace "newValue" with the new difficulty.

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

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

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

How to assign slider to float: Spin, on a gameobject 1 Answer

Variable from Void not working 1 Answer

How to return a function 0 Answers

I need to activate a function in another script from a script on a different game object 0 Answers

How to give Vector3 position to object being instantiated 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