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 /
avatar image
1
Question by Mesuko · Apr 28, 2017 at 05:50 AM · c#variableclassmethod

Why the variable don't change your value?

Hey guys,

There is a noob question but I don't find any place the answer.

The first script is just a variable and a method. The second is a method from another class.

What I don't understand is why the variable (first code) not change your value after the method. Thanks for the help. PS: I can't use static variables.

FIRST CODE

     public int enter = 5;
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.T))
             meth.calculate(enter);
     }


SECOND CODE

 public static void calculate(int number)
 {
     number -= 1;
 }
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
Best Answer

Answer by Ruri-Dev · Apr 28, 2017 at 05:12 PM

The method on the SECOND CODE is changing a local variable which scope extends to the method only. When you execute meth.calculate(enter) you're passing enter as a parameter and since an int is a value type it's value is copied and then passed to the method. There are two simple fixes:

  1. You change: calculate(int number) to calculate(ref int number)

  2. You change:

public static void calculate(int number) { number -= 1; }

to

public static int calculate(int number) { return number - 1; }

which is now returning an int, then call it on FIRST CODE as follows: enter = meth.calculate(enter);


Choose the one that suits you best.

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 Mesuko · Apr 28, 2017 at 09:58 PM 0
Share

That's answers all my questions. Thank you so much, you really helped me.

avatar image NoseKills · Apr 29, 2017 at 09:21 AM 2
Share

The sentiment in the answer is right but as @Bunny83 corrected me, I shall correct you :)

This does not happen because int is a value type. The same happens with reference types and it happens because method parameters are always passed by value unless you use the ref or out keywords.

If public int enter = 5; was ins$$anonymous$$d public GameObject enter = new GameObject("A"); (a reference type) , the same would still happen: changing what GameObject number references inside the method would not change what the field GameObject enter references.

Longer explanation in the comments here

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

8 People are following this question.

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

Related Questions

Why do I get this error: "Assets/Scripts/Parkour Movement.cs(15,38): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected" 2 Answers

public class variable in inspector like other components? 1 Answer

Access to a variable inside a C# class 2 Answers

[C#] Is there a way to call static variables directly, independent from parent class? 3 Answers

How to Typecast JS Variables as C# Classes? 0 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