How to link gameObject with backend

I have a script that connect to a web API to retrieve data and build a structure with different object.
Each object has a GameObject as a property.
Like this :

public class MyObject {
        public GameObject theGameObject;
        [...]
}

I would like to access the MyObject instance by clicking the corresponding GameObject.
How can I do that ?

I already tried to attach a script only containing a MyObject instance like this :

public MyObjectRetriever : MonoBehaviour {
        public MyObject myObject { get; set; }
}

but when I try to access the property via a GetComponent() call it returns NULL.

Hello there,

Assuming your GameObject gets properly assigned, the easiest way is to find it from a list. If you don’t have a list containing all your MyObject instances, you can always do this:

List<MyObject> list_myObjects = GameObject.FindObjectsOfType<MyObject>().ToList();

For this to work, you’ll have to include using System.linq at the top.

Then you can go through all of your instances and try to find the one with the corresponding GameObject like this:

            List<MyObject> list_myObjects = FindObjectsOfType<MyObject>().ToList();
            MyObject result = null;
            foreach (MyObject item in list_myObjects)
            {
                if(item.theGameObject.Equals(gameObjectIJustClickedOn))
                {
                    result = item;
                    break;
                }
            }
            if(result != null)
            {
                Debug.Log("I've found a MyObject instance with the GameObject you clicked on!");
            }
            else
            {
                Debug.LogWarning("NULL: There is no MyObject instance with the GameObject you clicked on...");
            }

Alternatively, since you have access to System.linq, you could also reduce the foreach to a single line:

            List<MyObject> list_myObjects = FindObjectsOfType<MyObject>().ToList();
            MyObject result = list_myObjects.Find(entry => entry.theGameObject.Equals(gameObjectIJustClickedOn));
            if(result != null)
            {
                Debug.Log("I've found a MyObject instance with the GameObject you clicked on!");
            }
            else
            {
                Debug.LogWarning("NULL: There is no MyObject instance with the GameObject you clicked on...");
            }

I hope that helps!

Cheers,

~LegendBacon

Instead of a list to search I’ve found a better way :

MyObject now extends ScriptableObject, I set the MyObject property in the component after this object is instantiated (and not before as I previously did) and now I can access the MyObject instance by calling :

MyObject obj = activeGameObject.GetComponent<MyObjectRetriever>().GetMyObject();

MyObject now looks like this :

 public class MyObject : ScriptableObject {
         public GameObject TheGameObject { get; set;}
         [...]
         public void Init(){
                 [...]
                 this.TheGameObject = GameObject.Instantiate(this.TheGameObject, this.Position, this.Rotation)
                 this.TheGameObject.GetComponent<MyObjectRetriever>().SetMyObject(this);
                 [...]
         }
 }