Search Unity

Copy class as data without reference

Discussion in 'Scripting' started by pqmt4fyt, Jun 25, 2019.

  1. pqmt4fyt

    pqmt4fyt

    Joined:
    Jun 17, 2019
    Posts:
    9
    I have two identical fields that are factually identical, but they're Transform and copying one to the other will link them together. How do I copy one variable's contents into another without reference cnonnection?

    MemberwiseClone() prompts an error:

    ....(19,30): error CS1540: Cannot access protected member 'object.MemberwiseClone()' via a qualifier of type 'List<Transform>'; the qualifier must be of type '......' (or derived from it)
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
  3. pqmt4fyt

    pqmt4fyt

    Joined:
    Jun 17, 2019
    Posts:
    9
    Provided solution relies on "ICloneable", which doesn't seem to exist by default in C#. Couple other StackOverflow answers provided that "ICloneable is considered a bad API now, since it does not specify whether the result is a deep or a shallow copy. I think this is why they do not improve this interface."

    If anybody is looking for answer it's second one:
    List<YourType> newList = new List<YourType>(oldList);
     
    NiCl2 and jvo3dc like this.