Generating ID to created object

I am new in unity. I have a code that creates a multiple cubes. I want to create for each of them unique ID but when I stop playing the game, the ID will be deleted with cube. I don’t now how to solve this problem. Can someone help me with it?

OK, you may be new to Unity but the code tells me you’ve done a bit of coding before.

Since you want to save quite a bit of data, you’ll need to use JSON for now. There are a few concepts that you’ll need to know (or research) before: JSON; File and directory naming; statics in C#; Serialisation; Classes that don’t inherit from monobehaviour; communicating between classes and gameobjects.

Having said that there are plenty of good videos on this topic. One I recommend to help you with the principles is this one by the always excellent Mike from Comp-3 Interactive. If you follow that, you’ll see how to save and save the data in a separate class to disk. If you can save all your cube data in a similar class, it’s an easy job to change from your code to implement JSON load/save.

Good luck!

This sounds, in principle, exactly the same as Inventory systems, user preferences, saved game states or even checkpoints. Even for hyper-casual games, there is always something to be saved. So, how to do it?

If you are a beginner, I don’t recommend getting into databases just yet. They may be your solution in time (whether saved locally, in the cloud or on the IOS/Android platform data store). The easiest way to start is using Player Preferences (PlayerPrefs). You haven’t said how many cubes there are and PlayerPrefs is not the best if you have tens of objects (it will cope but it will be messy). The main problem with PlayerPrefs is that there is limited range of data types and an array is not one of them, though you could create a very long string with names in it.

If PlayerPrefs don’t work, you may need to use something like JSON to write to a local file. Again, for a beginner, you will need to learn a fair amount and it’s too big a topic for this reply.

Just one thing, though. Is the game deterministic? By that I mean does the first cube always have the same name, likewise second cube etc? If that’s the case then rather than store the names, just recreate them each time. Perhaps you have a class with all the names written out, which will be very quick and not complex.

We probably need to hear more about what you’re trying to do to give a coded solution.

The whole code looks something like this. Every block have the same name.


public Material material;

[Range(0.5f, 1.0f)]
public float blockSize;

private int64 blockSizeScaled;

[Range(0.0f, 5.0f)]
public float simplifyEpsilonPercent;

[Range(1, 100)]
public int resolutionX = 11;

[Range(1, 100)]
public int resolutionY = 10;

public float depth = 1.0f;

private float width;

private float height;

private DestructibleBlock[] blocks;

private void Awake()
    BlockSimplification.epsilon = (int64)(simplifyEpsilonPercent / 100f * blockSize * VectorEx.float2int64);

    width = blockSize * resolutionX;
    height = blockSize * resolutionY;
    blockSizeScaled = (int64)(blockSize * VectorEx.float2int64);


public void Initialize()
    blocks = new DestructibleBlock[resolutionX * resolutionY];

    for (int x = 0; x < resolutionX; x++)
        for (int y = 0; y < resolutionY; y++)
            List<List<Vector2i>> polygons = new List<List<Vector2i>>();

            List<Vector2i> vertices = new List<Vector2i>();
            vertices.Add(new Vector2i { x = x * blockSizeScaled, y = (y + 1) * blockSizeScaled });
            vertices.Add(new Vector2i { x = x * blockSizeScaled, y = y * blockSizeScaled });
            vertices.Add(new Vector2i { x = (x + 1) * blockSizeScaled, y = y * blockSizeScaled });
            vertices.Add(new Vector2i { x = (x + 1) * blockSizeScaled, y = (y + 1) * blockSizeScaled });


            int idx = x + resolutionX * y;

            DestructibleBlock block = CreateBlock();
            blocks[idx] = block;

            UpdateBlockBounds(x, y);

            block.UpdateGeometryWithMoreVertices(polygons, width, height, depth);

public Vector2 GetPositionOffset()
    return transform.position;

private DestructibleBlock CreateBlock()
    GameObject childObject = new GameObject(); = "DestructableBlock";
    childObject.transform.localPosition =;

    DestructibleBlock blockComp = childObject.AddComponent<DestructibleBlock>();

    return blockComp;

private void UpdateBlockBounds(int x, int y)
    int lx = x;
    int ly = y;
    int ux = x + 1;
    int uy = y + 1;

    if (lx == 0) lx = -1;
    if (ly == 0) ly = -1;
    if (ux == resolutionX) ux = resolutionX + 1;
    if (uy == resolutionY) uy = resolutionY + 1;

    BlockSimplification.currentLowerPoint = new Vector2i
        x = lx * blockSizeScaled,
        y = ly * blockSizeScaled

    BlockSimplification.currentUpperPoint = new Vector2i
        x = ux * blockSizeScaled,
        y = uy * blockSizeScaled

public void ExecuteClip(IClip clip)
    BlockSimplification.epsilon = (int64)(simplifyEpsilonPercent / 100f * blockSize * VectorEx.float2int64);

    List<Vector2i> clipVertices = clip.GetVertices();

    ClipBounds bounds = clip.GetBounds();
    int x1 = Mathf.Max(0, (int)(bounds.lowerPoint.x / blockSize));
    if (x1 > resolutionX - 1) return;
    int y1 = Mathf.Max(0, (int)(bounds.lowerPoint.y / blockSize));
    if (y1 > resolutionY - 1) return;
    int x2 = Mathf.Min(resolutionX - 1, (int)(bounds.upperPoint.x / blockSize));
    if (x2 < 0) return;
    int y2 = Mathf.Min(resolutionY - 1, (int)(bounds.upperPoint.y / blockSize));
    if (y2 < 0) return;

    for (int x = x1; x <= x2; x++)
        for (int y = y1; y <= y2; y++)
            if (clip.CheckBlockOverlapping(new Vector2f((x + 0.5f) * blockSize, (y + 0.5f) * blockSize), blockSize))
                DestructibleBlock block = blocks[x + resolutionX * y];

                List<List<Vector2i>> solutions = new List<List<Vector2i>>();

                ClipperLib.Clipper clipper = new ClipperLib.Clipper();
                clipper.AddPolygons(block.Polygons, ClipperLib.PolyType.ptSubject);
                clipper.AddPolygon(clipVertices, ClipperLib.PolyType.ptClip);
                clipper.Execute(ClipperLib.ClipType.ctDifference, solutions,
                    ClipperLib.PolyFillType.pftNonZero, ClipperLib.PolyFillType.pftNonZero);

                UpdateBlockBounds(x, y);

                block.UpdateGeometryWithMoreVertices(solutions, width, height, depth);