(C#) How to copy classes by value type and not reference type?
问题 Take the following code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class MyClass { public int myNumber; } public class Test : MonoBehaviour { MyClass class1 = new MyClass(); MyClass class2 = new MyClass(); void Start() { class1.myNumber = 1; class2 = class1; class2.myNumber = 2; Debug.Log(class1.myNumber); //output: 2 (I'd want it to output 1!!!) } } Now, if I understood correctly, in C# classes are a reference type and not a value type, so this