Search Unity

GetInstanceID and ScriptableObject problem...

Discussion in 'Scripting' started by MedalHellWay, May 15, 2019.

  1. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Hi everyone! I write here because I've a problem that I can't solve heheeheh :)

    I created a ScriptableObject that allows me to store information about the gameObject where it is attached. In addition, with the use of GetInstanceID I determined always a unique number for the gameobject. So far, so good.

    I created a canvas with name and description where it is a parent of the gameobject and is activated by the player. When I take the object, I have the decription and name of the gameobject.
    To access every single gameobject and its features, I used a loop with a ID test (if the ID number matches the gameobject). So, If the ID number is the same as in the game object, activate the canvas with its features.
    So far so good except for one problem; I'm only shown the canvas of a single object and not of the others! Where am I wrong?


    test01.jpg
    this object show full information with unique ID ecc. and debug.

    test02.jpg
    this object shows no information even debugging

    In script Player, I've used For loop for check compare ID and activate Canvas:

    Code (CSharp):
    1.                         if (checkUIdiInfoOb.checkID == attachUniqueID)
    2.                         {
    3.                             canUI.GetComponent<Canvas>().enabled = true;
    4.                             Debug.Log("vero" + attachUniqueID + " == " + checkUIdiInfoOb.checkID + "nome oggetto " + go[i].name);
    5.                         }

    I've attached all the scripts I used

    Thanks fo any help :)
     

    Attached Files:

  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    InstanceID is valid only during one player or editor session. If you restart your game, everything is lost. You need kinda your own identity system because there's no anything for this in Unity.
     
    MedalHellWay likes this.
  3. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    But if I use a double check to determine the equality ID, it can vary over time but always has a match or mistake?

    Any alternative tip?
     
  4. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Update

    I was able to solve the problem. Virtually failing to get into the canvas I used an additional For loop with an additional ID check (scriptableObject + gameObject + canvas). Now everything works well but I don't know if this system is the right one. Can you better adjust this type of loop search? (forgive me, but I'm a script amateur)

    Below the example that solved the problem for me:

    Code (CSharp):
    1.  
    2.                         for (int c = 0; c < checkUIdiInfoOb.Length; c++)
    3.                         {
    4.                             if (checkUIdiInfoOb[c].checkID == attachUniqueID)
    5.                             {
    6.  
    7.                                 for (int s = 0; s < canUI.Length; s++)
    8.                                 {
    9.                                     if (checkUIdiInfoOb[s].checkID == attachUniqueID)
    10.                                     {
    11.                                      
    12.                                         canUI[s].gameObject.GetComponent<CanvasUtility>().CanUtility.enabled = true;
    13.                                     }
    14.                                 }
    15.                             }
    16.                        }
    17.  
    Thanks again for any tup :)

    Edit: I noticed that the ID with this check does not give any problems, either in the editor or in the Game. I have tried several times and always successfully!
     
    Last edited: May 16, 2019
  5. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Big PROBLEM!!

    Kids all worked great yesterday. This morning everything worked very well. Today afternoons it's all broken !! Start Unity and CHeckID is broken!! Especially this check:

    Code (CSharp):
    1.     public GrabObj goID;
    2.     public int checkID;
    3.  
    4.     void Start()
    5.     {
    6.         goID = gameObject.GetComponentInParent(typeof (GrabObj)) as GrabObj;
    7.  
    8.         if (goID != null)
    9.         {
    10.             checkID = goID.NumbID;
    11.             Debug.Log(goID.NumbID);
    12.         }
    13.     }
    It always returns me "Zero"!! Arghhhhhh!!!

    GrabObj is a script for gameobject with all info about it. Inside I also have an ID control called "NumbID" and work well!!!

    Code (CSharp):
    1. public int NumbID; //ecc.
    2.  
    3. NumbID = obj.GetInstanceID();
    I restarted Unity and Visual Studio many times, but it doesn't work anymore .. how is that possible ?? Please HELP!!!