Search Unity

Uniquely identifying objects

Discussion in 'Scripting' started by warrenbrandt, Sep 10, 2020.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    if i have 3 destinations that are separate objects
    and each has a script on
    called LandingPad1 LandingPad2 etc
    how can i make a script somewhere else detect which is which?

    i pulled the variable string into my working script to then compare the original destination, but that doesnt work. Im guessing the variable on the object is meant to be read.

    Whats the most logical way of uniquely identifying objects in Unity?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Are you comparing the public string field in the target identifying script with the passenger's string? It's just two strings compared... it should compare just fine. If it fails then those strings are different.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    "Telling which is which" normally implies that you are comparing them to something. For example, someone gives you a command like "use landing pad #2" and so then you need to figure out which one is "#2". What are you comparing them against?

    If you have a choice of what you're comparing them against, then often the best option would be to pass around direct references to the object and skip the "compare" step entirely. I.e. instead of "use landing pad #2", it says "use the landing pad stored in this variable".

    There is also GetInstanceID() but it's actually pretty hard to think of circumstances where that would be better than an actual reference.

    It is also possible (but usually less efficient and less reliable) to compare objects by name or by tag.