- Home /
I have an array in Unity. I want to assign an image based on the gameobject's tag
This is in Unity 2018.4.20f LTS. I have a lot of 3d gameobjects that I'm storing in an Array. I have searched around but can't find a way to do it. Right now I have a panel and a bunch of white box images as placeholders. When I pick up an object a white box pops up on my canvas panel.
What I would like to know is there a way to grab an image and display it in place of the white place holder image based on the gameobject's tag? Like if it is tagged hammer and I have a hammer image display that image?
If so could someone point me in the right direction on how I would accomplish this?
gameObject.tag is a string and it is easy to get it through code. How are you trying to load the image and fails? There are many ways of doing that like Resources.Load, or get from a List etc.
Answer by Rick8684n · May 04, 2020 at 12:52 PM
Can you use a String to reference the object's tag? I needed to instantiate objects into an array based on their tags, and I did it something like:
GameObject OriginalObject = Resources.Load("Folder/" + PrefabReference) as GameObject;
The PrefabReference just being a String with the name of the prefab. Then just instantiate OriginalObject to the array:
ObjectArray[number] = Instantiate (OriginalObject, new Vector3(1f,0,20f), Quarternion.Identity);