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 Wasplay · May 08, 2018 at 04:44 PM · serializationvariablesclasses

Custom classes having a strange behaviour

Hello,

I have two variables of type "PlayerControls"

     [System.Serializable]
     public class PlayerControls
     {
 
         public float horizontal;
         public float vertical;
         public bool down;
         public bool up;
         public bool left;
         public bool right;
         public bool boost;
         public bool interact;
         public bool jump;
         public bool start;
     }

And after doing that :

     void Start () {
         gm.prevInputs = gm.inputs;
     }

The variable "prevInputs" keeps being synchronised with the "inputs" variable, what am I doing wrong ?

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 MacDx · May 08, 2018 at 05:14 PM 0
Share

What do you mean by keeps being synchronized? You're clearly assigning gm.inputs into gm.prevInputs , so I'm not understanding what you're expecting to happen and what's wrong.

2 Replies

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

Answer by Wasplay · May 08, 2018 at 09:28 PM

It's working ! Thank you so much ! Here is what i've done :

 if (buffer) 
 { 
     GetInput(gm.buffer1); 
     buffer = false; 
     gm.inputs = gm.buffer1; 
     gm.prevInputs = gm.buffer2;
 } 
 else 
 { 
     GetInput(gm.buffer2);
     buffer = true;
     gm.inputs = gm.buffer2; 
     gm.prevInputs = gm.buffer1; 
 }
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
avatar image
3

Answer by MacDx · May 08, 2018 at 05:22 PM

Hmmm I think I know what you're having trouble with now.

Do you know the difference between value types and reference types?

Value types are types (like int,floats,enums,structs, etc.) that when you assign them to a field/variable or pass them to methods, their value will get copied, so if you modify them nothing will happen to the original.


On the other hand, reference types (any class type) will not get copied when you pass them to a method or assign them to variables, instead, you are simply passing a reference so anyone that has that reference will modify the same object.


I'm not sure if my explanation was clear enough so here's a link that provides further detail. http://net-informations.com/faq/general/valuetype-referencetype.htm


So what you're calling strange "synchronization" behaviour is just the normal behaviour of the C# language (and many other modern programming languages). If you just don't want this to happen, you will have to change your PlayerControls class into a struct like this:

 [System.Serializable]
 public struct PlayerControls //Structs are value types
 {
     public float horizontal;
     public float vertical;
     public bool down;
     public bool up;
     public bool left;
     public bool right;
     public bool boost;
     public bool interact;
     public bool jump;
     public bool start;
 }

Hope this helps!

Comment
Add comment · Show 7 · 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 Wasplay · May 08, 2018 at 07:45 PM 0
Share

Same problem :/ I've changed the class to struct but prevInputs = inputs; at start still results in a synchronisation between the two PlayerControls :(

avatar image Bunny83 Wasplay · May 08, 2018 at 08:24 PM 1
Share

No, that's impossible. You either did not save your changes or you actually have such a copy line somewhere else in Update.

avatar image Wasplay Bunny83 · May 08, 2018 at 08:34 PM 0
Share

I'm uploading a short video to show you the project, I did everything correctly idk why this isn't working :(

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

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

Should we always cache local variables in classes? 0 Answers

Saving editor-only variables 0 Answers

Save an array with custom classes 1 Answer

Missing Documentation on accessing Variables 1 Answer

Saving class data without having lots of references? 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