Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to access a Guid array?

Discussion in 'Scripting' started by Nachman, Dec 10, 2021.

  1. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    Hi,
    I am having issues trying to access and populate a Guid array. I can't either see the public array on the inspector or add any elements to it. The class is inherent of MonoBehaviour, I tried the Serialized field attribute, and still, I can't even see it on the inspector. Any ideas of what I am doing wrong?

    Thanks any help will be greatly appreciated.

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7.   public class GlobalToolbarViewModel : MonoBehaviour {
    8.     public bool duplicateActive;
    9.     public bool alignActive;
    10.     public bool forwardActive;
    11.     public bool backwardActive;
    12.     public bool rulersActive;
    13.     public bool undoActive;
    14.     public bool redoActive;
    15.     public bool clearAllActive;
    16.     public bool saveActive;
    17.     public bool goToCartActive;
    18.     public bool deleteActive;
    19.     public bool infoActive;
    20.  
    21.    [SerializeField]
    22.     public Guid[] activeObjects;
    23.        
    24.      
    25.  
    26.  
    27.         public void ShowObjects()
    28.         {
    29.             Debug.Log(activeObjects);
    30.         }
    31.  
    32.      
    33.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    The GUID class is not itself serializable by Unity, so it will not show up. You could make a String[] and serialize that if you want, and use the string constructor https://docs.microsoft.com/en-us/do...view=net-6.0#System_Guid__ctor_System_String_ and the ToString() method to convert back and forth.

    You could also make your own custom inspector for your MonoBehaviour here that uses those techniques to display and update an actual Guid[]
     
  3. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    Ok thanks, but one quick question, since what I really need to do is to be able to populate the array from another class, how should I go about it? because the code doesn't work, the array stays null.

    Code (CSharp):
    1.  
    2.         GlobalToolbarViewModel globaltoolbarviewModel;
    3.         public DesignObject designObject;
    4.  
    5.         public void SelectNewObject()
    6.         {
    7.            DesignObject designObject = currentState.outsideDesignState.designObjects[currentState.designObjectIndex];
    8.        
    9.            designObject = designObject.guid
    10.  
    11.            globaltoolbarviewModel.activeObjects[0] = designObject.guid;
    12.  
    13.          
    14.          
    15.         }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    You need to allocate an appropriately-sized array first.

    Arrays can't change size once allocated.

    You may find it easier to use a List<T> if you plan on ad-hoc adding to the array.
     
  5. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    I tried allocating the array size already and it didn't work either.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    Show what you tried and what errors you ran into.
     
  7. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    Thanks, Apparently it is populating the array but the Guid looks like this:

    00000000-0000-0000-0000-000000000000

    It is very strange because

    Debug.Log(designObject.guid);

    Does show the correct Guid, but it passes that zero out Guid. Any ideas?

    Thanks.
     
  8. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    I figure it out, Thanks!
     
  9. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,882
    It's customary to share the solution you found yourself, so folks in 2036 using Google brain implants can also learn how to solve similar problems.
     
  10. Serge_Billault

    Serge_Billault

    Joined:
    Aug 26, 2014
    Posts:
    190
    For brain implants I'm curious about how they anticipate problems like the brain growing in size over age, and people walking behind you with a big magnet just to piss you off, when simply not working at an electrical power plant or near a train transformer and high voltage heavy duty transfos. If I remember correctly it's Russia that has 400Km wide nav system jaming devices, which, for the moment, do not target civilian devices.
     
    Last edited: Dec 10, 2021
  11. Nachman

    Nachman

    Joined:
    Oct 22, 2013
    Posts:
    26
    I am sorry! The problem was very simple but this is a refactoring project from somebody else and It's driving me crazy.
    So I was passing the wrong designObject variable, it had to be the local one instead, that's why it was a Guid of zeros.

    Anyway thanks, I'll need one of those brain implants myself!