0

for example:

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

public class CrateState : MonoBehaviour, IStateQuery
{
    public Transform crateCap;
    public SaveLoad saveLoad;
    public bool hasOpened;

    private bool saveOnce = true;

    private State m_state = new State();

    public Guid UniqueId = new Guid();//=> Guid.Parse("D9687717-CEF7-4E6C-8318-20BCC51E0110");

    private class State
    {
        public bool open;
    }

    public string GetState()
    {
        return JsonUtility.ToJson(m_state);
    }

    public void SetState(string jsonString)
    {
        m_state = JsonUtility.FromJson<State>(jsonString);

        if (m_state.open)
        {
            crateCap.localRotation = Quaternion.Euler(90, 0, 0);

            hasOpened = true;
        }
        else
        {
            crateCap.localRotation = Quaternion.Euler(180, 0, 0);

            hasOpened = false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (GetComponent<UnlockCrate>().HasOpened())
        {
            hasOpened = true;

            if (saveOnce)
            {
                StartCoroutine(saveLoad.SaveWithTime());
                saveOnce = false;
            }
        }
        else
        {
            hasOpened = false;
        }

        m_state.open = hasOpened;
    }
}

The line :

public Guid UniqueId = new Guid();

was :

public Guid UniqueId => Guid.Parse("D9687717-CEF7-4E6C-8318-20BCC51E0110");

but it's a bit annoying to do in the menu in the visual studio: Tools > Create GUID > and then generate a new guid and copy it to the script.

I'm using this script base for every object I want to save its state. and for that i need to generate a new guid.

I tried to change the line to:

public Guid UniqueId = new Guid();

but then getting error on the top at: IStateQuery

CrateState does not implement interface member IStateQuery.UniqueId

This is the IStateQuery class:

using System;

public interface IStateQuery
{
    string GetState();
    void SetState(string jsonString);

    Guid UniqueId { get; }
}

i'm not sure if making instance like i'm doing now is the right way and how ot fix the error. in the other hand i want to make something easier to generate each time a new guid number instead copying it manual to the state script.

I tried to make a new instance:

public Guid UniqueId = new Guid();

but this give me error on the IStateQuery so I'm not sure if this is the right approach.

  • Does this answer your question? [C# how to create a Guid value?](https://stackoverflow.com/questions/2344098/c-sharp-how-to-create-a-guid-value) – Serlite Feb 12 '23 at 22:08

2 Answers2

1

Give this a shot:

[SerualizeField] private string _uniqueId = string.Empty;
public Guid UniqueId
{
    get
    {
        if (string.IsNullOrEmpty(_uniqueId))
            _uniqueId = Guid.NewGuid().ToString();
        return Guid.Parse(_uniqueId);
    }
}

Unity can’t serialise a Guid, but it can serialise a string. So, we can use a string as the underlying guid store. Unity could also serialise a byte[], which would be more efficient, especially if UniqueID is read many times. The code above is just the simplest to implement and easiest to understand, and if you’re serialising to JSON, then a string Id is likely going to be the most helpful.

Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
  • 1
    You should Initialize that SerializeField in `OnValidate` and `Reset` so it is serialized on editor time before running the app and first access to the property .. currently this would only be initialized the moment you first access the property which seems to be too late for OPs use case – derHugo Feb 13 '23 at 09:05
0

You are initializing a read-only property using the Lambda operator.

Change

public Guid UniqueId => Guid.Parse("D9687717-CEF7-4E6C-8318-20BCC51E0110");

to

public Guid UniqueId => Guid.NewGuid();

Guid.NewGuid is a static method of the Guid Struct.

See also

Stack Overflow: C# how to create a Guid value?

Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14