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
0
Question by losingisfun · Sep 25, 2016 at 03:49 PM · stringfunctionsparsing

C# - parsed strings not changing in function

I have a function, for the simplicity and density of this question, lets say it is

 void DoSomething (int number, string word) {
     number = 10;
     word = "hello";
 }

when i do this, the int argument variable is changed, but the string argument never gets changed. I've tracked it down to this function. Its been happening to me a lot, but i usually find a work-around. Is it normal that strings don't get changed from being parsed? if so.... can someone explain why?

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 Bonfire-Boy · Sep 25, 2016 at 06:56 PM 2
Share

I think you need to show us a bit more. In the code you've posted, clearly both the variables will change when you set them, and there's nothing that could suggest otherwise since they're local and thus disappear as soon as the function finishes.

1 Reply

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

Answer by NoseKills · Sep 25, 2016 at 09:05 PM

Either I don't understand the question or the example is flawed.

Int is a value type and string acts like a value type in this respect so in this example you can't change either of the ORIGINAL variables by passing them into a method like this.

 void Awake() {
     int myNumber = 1;
     string myString = "goodbye";
     DoSomething(myNumber, myString);
 
     Debug.Log(myNumber + ", " + myString);
     // Logs out "1, goodbye"
 }
 
 void DoSomething (int number, string word) {
      number = 10;
      word = "hello";
  }

When you call a method with value type parameters, the value of the passed in variables gets copied and passed into the function, so inside the function you are changing just a copy of the original variables.

When you call a method with a reference type parameter such as GameObject, a reference to the original GameObject is passed into the function, so if you for example change the name of the GameObject, you'll see the change wherever you have a reference to the same GameObject

String is actually a reference type but in this respect it acts like a value type.

Value type vs. reference type in C#

Comment
Add comment · Show 5 · 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 · Sep 25, 2016 at 09:42 PM 0
Share

This is actually not a matter of value type vs reference type but a matter of passing by value vs passing by reference. Both examples you've given are actually passed by value which is the default for all method calls unless specified otherwise.

It is true that you can change a reference type object through a copied reference, but you can't replace the reference itself since the "reference" is the "value" of a reference type variable.

That's what the keywords "ref" and "out" are good for. They actually pass the variable by reference, no matter what type the variable has:

 void DoSomething(ref int number, ref string word)
 {
     number = 10;
     word = "hello";
 }
 
 void Awake() {
     int myNumber = 1;
     string myString = "goodbye";
     DoSomething(ref myNumber, ref myString);
 
     Debug.Log(myNumber + ", " + myString);
     // Logs out "10, hello"
 }
avatar image losingisfun Bunny83 · Sep 26, 2016 at 01:01 AM 0
Share

This is very good to know, thank you. I was actually passing an int array and a string variable, i'm guessing arrays are reference types and not value types, therefore my array was getting changed but my string wasn't. Thanks, (I didn't post my whole slab of code because it was literally a 300 line function for randomly generating item stats, the string was just the name of the item.)

avatar image Bonfire-Boy losingisfun · Sep 26, 2016 at 10:35 AM 0
Share

Ah, yes, arrays are passed by reference, that explains it.

For future reference, when showing people the code that's causing a problem, it's best not to simplify it too much. Changing the type of a variable that you're asking about (an int is quite different to an arrays of int) is too much. It could have prevented anyone from diagnosing the issue.

avatar image Bonfire-Boy · Sep 25, 2016 at 09:54 PM 1
Share

What's puzzling me is the OP's saying that things are working differently for the int compared to the string. $$anonymous$$aybe that's just a mistaken observation.

avatar image Bunny83 Bonfire-Boy · Sep 26, 2016 at 12:06 AM 0
Share

Yes, for sure ^^. As i said the variable type doesn't matter. If the parameter is not a "ref" parameter, a method can't change the variable itself that is passed to the method. Llike Nose$$anonymous$$ills said, parameters are local variables to the method and only contain a copy of the "value" of the passed variable.

Further more non-ref-parameters can be literal values (constants). So the original method could be called like this: DoSomething(5, "FooBar"); Here it seems quite clear that the value is just copied into the local parameter variable when the method is called.

A ref parameter on the other hand always needs to be a variable. "ref" parameters don't pass the value of a variable along, but a managed pointer to the actual memory where the variable is located. So non-ref-parameters always pass along the "content" or a variable while ref-parameters allow to pass the variable itself.

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

55 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a string to call a function from another script without "SendMessage" 2 Answers

How to streamread and parse. 0 Answers

Making an in-game command line. How do I get arguments? 1 Answer

How to convert a string to an int? 1 Answer

How can i split a string[] ? 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