-1

The original goal is to make an editor script and only when attaching the mono script to an object to make that it will generate the new guid but not sure how to do it. So for now I'm using the ExecuteInEditMode attribute.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class GenerateAutomaticGuid : MonoBehaviour
{
    public string guid;

    private Guid uniqueID;

    // Start is called before the first frame update
    void Start()
    {
        uniqueID  = new System.Guid();
        guid = uniqueID.ToString();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

The result in the string is : 00000000-0000-0000-0000-000000000000 and it's executing when getting back to the editor and not only when attaching the script to a gameobject.

The main goal is to generate a unique id to each gameobject the script will be attach to.

I need it since the gameobjects instance id's change each time I'm loading a saved game. The unity generate new id's for the objects and the id/s on the saved game file is not the same.

Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 1
    Does this answer your question? [Guid is all 0's (zeros)?](https://stackoverflow.com/questions/7972150/guid-is-all-0s-zeros) – James Thorpe Oct 07 '20 at 16:09

1 Answers1

3

That's what new Guid() does; use Guid.NewGuid() instead

Caius Jard
  • 72,509
  • 5
  • 49
  • 80