Daniel Lip
Daniel Lip

Reputation: 11321

How can I generate a guid and assign it to a variable?

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

public class GenerateState : MonoBehaviour, IStateQuery
{
    private State m_state = new State();
    private Guid guidID;

    private void Awake()
    {
        guidID = Guid.NewGuid();
    }

    public Guid UniqueId => Guid.Parse(guidID.ToString());

    private class State
    {
        // Properties
    }

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

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

It does generate a guid inside the Awake but I want to assign the guid after it created to the UniqueId variable at line 16 :

public Guid UniqueId => Guid.Parse(guidID.ToString());

but it's never getting to it. I'm using the UniqueID also in other scripts that's why I used =>

Upvotes: 1

Views: 646

Answers (1)

Tan Sang
Tan Sang

Reputation: 2069

I want to assign the guid after it created to the UniqueId variable

You mean this?

public class GenerateState : MonoBehaviour, IStateQuery
{
    /*
     other implementation 
    */

    private Guid myGuid;
    
    public Guid UniqueId => myGuid = Guid.NewGuid();
}

Upvotes: 3

Related Questions