0

I have a SO that contains some data:

[CreateAssetMenu(fileName = "Map Information", menuName = "Map", order = 0)]
public class Map : ScriptableObject
{
    public Terrain[] tiles;
}

Terrain is a class:

public class Terrain
{
 //things and functions
}

the problem is that when I get a reference of this SO and pass some of this terrains to other classes and scripts, when I edit those the SO keep those changes, i have tried making Terrain a struct but it doesn't work and keep keeping changes.

Yoaquin
  • 1
  • 1

1 Answers1

0

By default, C# passes objects by reference, meaning all references of an object in your code will be updated when you make changes to one of them, if you'd like a clone of the Terrain then you can add the following function to your Terrain class:

public Terrain Clone()
{
    return (Terrain)this.MemberwiseClone();
}

After that, if you need a terrain from the array without having any of the changes affect the original terrain then you can use something like:

Terrain terrain = Terrain[0].Clone();
// the original terrain will remain unaffected if any changes are made to this
Hazeral
  • 16
  • 4