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 putragonist · Aug 22, 2016 at 05:01 AM · assign-variable

[Solved] Assigning variable without changing the value of assigned variable when the variable value is changed?

Hello, I have a problem with variable declaration. I already google it but doesn't really know what the keyword would be and don't find the answer, so I try to ask in this place instead. Sorry if the title is hard to understand..

So, I have this script:

  public static CharacterDurability CountBoundary(CharacterDurability durability,PlayerHealth health)
     {
         CharacterDurability temp = durability;
         CharacterDurability temp2 = durability;
         CharacterDurability temp3 = temp2;

         Debug.Log("durability1 =" + durability.stamina);
 
         temp.stamina = durability.stamina + (durability.stamina * (health.Mood / 100)) * 20 / 100;
 
         Debug.Log("durability2 =" + durability.stamina); 
         Debug.Log("temp2 =" + temp2.stamina);
         Debug.Log("temp3 =" + temp3.stamina);
         // durability changed after using formula above. I dunno how to make things work. Help!!
         return temp;
     }

The output is durability1 = 80 durability2 = 92 temp2 = 92 temp3 = 92

And then I changed the formula to

 temp.stamina = temp.stamina + (temp.stamina * (health.Mood / 100)) * 20 / 100;

But the value still same as before

I don't want the values of durability to be changed (still 80), but I want to use variable temp with value of durability assigned. How can I do that? Please help me...

I'm using 5.4.0f3 personal edition, with visual studio And using Wingdows 10 64 bit as OS...

Edit:

OK, I found the answer. What I need is change the temp declaration to this one

 CharacterDurability temp = (CharacterDurability) durability.MemberwiseClone() ;

And it all work now.

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
0

Answer by Ahndrakhul · Aug 22, 2016 at 07:20 AM

If I am understanding this correctly, I think that you can fix your problem by changing

 CharacterDurability temp = durability;

to

 CharacterDurability temp = new CharacterDurability();

As your code is right now, your three temp variables all reference the original durability instance of "CharacterDurability" that is passed into the method. Because of this, a change to one of the temp variables is a change to all of them. You need to create a new instance of CharacterDurability so that you can alter its variables (stamina in this case), without altering the stamina variable of the original.

Comment
Add comment · Show 1 · 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 putragonist · Aug 22, 2016 at 10:02 AM 0
Share

Thanks for the answer.

Yes, your way is not wrong, but I don't want to use new class since there will be so much variables in the class to be assigned and that will be hard to do maintenance. I already found the answer as above, which use $$anonymous$$emberwiseClone(), and it finally worked!!

And again, thank you for the answer

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

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

Related Questions

Variable not assigned but is. 0 Answers

FindGameObject assign in a variable? help! 2 Answers

Narrow Prefab Selection through attached Scripts 2 Answers

Variable won't update after assignment. 1 Answer

How do you assign the value of a ScriptableObject (from asset) to a component variable? 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