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

Can someone please explain how to used unique IDs?

Discussion in 'Scripting' started by RaulG, Feb 24, 2015.

  1. RaulG

    RaulG

    Joined:
    May 31, 2013
    Posts:
    74
    Been trying to figure this out for a good few months. Seems like I get trollish answers or error filled madness.. Or both, mostly both...

    I get the whole
    Code (CSharp):
    1. public int Id = 0;
    and then just adding 1 to it whenever an object is instantiated. But then it comes down to how would I use it?

    Im wanting to use IDs since I have an object that is spawned multiple times. So I end up with maybe 15 or so of this object, each with its own transform. And from each of these objects, more objects are spawned from them. The only solution was to parent the spawned objects to the main object, but it's now causing a bit more problems. So Im trying to find an alternate solution.
     
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    You haven't clearly mentioned whats the problem here.
    It looks unlikely, but is it that you are unable to access the
    ID values for some reason ?
     
  3. RaulG

    RaulG

    Joined:
    May 31, 2013
    Posts:
    74
    Yeah basically. I've tried a few ways like for loops and the such. It didn't really work..
     
  4. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Ok..Can you show those codes here ?
     
  5. RaulG

    RaulG

    Joined:
    May 31, 2013
    Posts:
    74
    Code (CSharp):
    1.     public GameObject circle;
    2.  
    3.     public eSpawn spawn;
    4.  
    5.     public List<GameObject> cirList = new List<GameObject>();
    6.  
    7.  
    8.     void Start()
    9.     {
    10.         GameObject spawnScript = GameObject.Find ("Spawner");
    11.         spawn = spawnScript.GetComponent<eSpawn> ();
    12.     }
    13.     void Update ()
    14.     {
    15.         cirList.Add (spawn.sClone);
    16.         Transform B = transform;
    17.         Vector3 newPosition = B.position - cirList [spawn.uniqueID].transform.position;
    18.        
    19.         Quaternion lookRotation = Quaternion.LookRotation (newPosition);
    20.         B.rotation = Quaternion.Slerp (B.rotation, lookRotation, Time.deltaTime * 5.0f);
    21.        
    22.         transform.position += newPosition * Time.deltaTime;
    23.         Destroy (gameObject, 2);
    24.     }
    Code (CSharp):
    1. void SpawnSpreader()
    2.     {  
    3.  
    4. public int uniqueID = 0;
    5.         if(spreaderLive <= 0.0f)
    6.         {
    7.             sClone = Instantiate (Spreader, new Vector3 (this.transform.position.x + shiftxPos,
    8.                                                          this.transform.position.y,
    9.                                                          this.transform.position.z),
    10.                                   Spreader.transform.rotation) as GameObject;
    11.             sClone.name = "CircleShot " +uniqueID;
    12.  
    13.             shiftxPos -= 20;
    14.             //bSpread.circle = sClone;
    15.             spreaderLive = 1.0f;
    16.             uniqueID+=1;
    17.             if(shiftxPos <= -20)
    18.             {
    19.                 shiftxPos = 10;
    20.             }
    21.         }
    22.  
    23.     }
    Thats my current attempt.
     
  6. Deleted User

    Deleted User

    Guest

  7. RaulG

    RaulG

    Joined:
    May 31, 2013
    Posts:
    74
    Yeah I've heard about that.

    Can't do anything with it though since it's always unique on each run.
     
  8. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    - Can you show how SpawnSpreader() is called ?

    - where is the uniqueID getting assigned ?

    - also, maybe it should be sClone.name = "CircleShot " + uniqueID.ToString();

    - above all, where are you accessing uniqueID which is creating problems ?
     
  9. RaulG

    RaulG

    Joined:
    May 31, 2013
    Posts:
    74
    Code (CSharp):
    1.    Vector3 newPosition = B.position - cirList [spawn.uniqueID].transform.position;
    Gives a Argument out of range error.

    Code (CSharp):
    1. void Update ()
    2.     {
    3.  
    4.         scatterLive -= Time.deltaTime;
    5.         spreaderLive -= Time.deltaTime;
    6.         spawnTimer += Time.deltaTime;
    7.  
    8.         SpawnSpreader ();
    9.         SpawnScatter ();
    10.         SpawnUpgrade ();
    11.  
    12.         if (xPos <= -10)
    13.         {
    14.             xPos = 5;
    15.         }
    16.     }    
    In the eSpawn script.
     
  10. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    Making your own unique ID system can be more trouble than it's worth. Unless you really need a custom solution for some reason, I always just take advantage of .NET's Guids:

    Code (CSharp):
    1. private void Awake()
    2. {
    3.     if (string.IsNullOrEmpty(guid))
    4.         guid = System.Guid.NewGuid().ToString();
    5. }
     
  11. Deleted User

    Deleted User

    Guest

    So you also need your Unique ID to be the same each run is what you're saying?
     
  12. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    Yeah, also this. Needing unique IDs between runs would suggest that you either want to maintain persistency across scenes, or you're trying to serialize the data from these objects. Is that correct?

    If so, the Guid solution I mentioned should work if you wrap that up in the appropriate Editor scripts or whatever. Otherwise, Unity's default identifiers should meet your needs, I think.
     
  13. RaulG

    RaulG

    Joined:
    May 31, 2013
    Posts:
    74
    Basically yeah. I though it was obvious the last few times I asked for something like this. This is actually the best answer I've gotten so far.

    Do you have any tutorials for GUID? Something that goes well into how to use it and how it works?
     
  14. LoftyTheMetroid

    LoftyTheMetroid

    Joined:
    Oct 26, 2012
    Posts:
    57
    Well, it does pretty much what you'd expect. It creates a globally unique identifier that you can use for whatever ID needs you might have. You can find documentation here.

    Since a GUID is a 128-bit integer, it's hard to manage it as an int since Unity will only serialize a regular int, which isn't large enough to contain the GUID. For me, it's easier to just store it as a string, which will serialize without a problem.

    My typical use case for a GUID involves needing to serialize references to certain data assets. For example, I have a base UniqueScriptableObject class that I usually extend for data assets. This class automatically generates a GUID for the asset when instantiated. When I need to serialize game state and have objects with UniqueScriptableObject references, I just serialize the GUID and not the rest of the data. Then, when deserializing, I have a Dictionary that let's me pull the appropriate data asset and assign a reference back to the appropriate object.

    That might be overkill for what you're doing, though...? I'm not exactly sure what your purpose is for the IDs, but hopefully that makes sense. Regardless, it should still work.
     
    Last edited: Feb 25, 2015