- Home /
images assigned to gameobjects by tag
This is in Unity 2018.4.20f LTS. I have a lot of 3d gameobjects that I'm storing in a list. 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?
Answer by shadowpuppet · May 03, 2020 at 07:09 PM
When you pick up the object something happens, right? like the object disappears from the world and is in your hand or something? How do you pick up the object? In my game , if I am within the triggerzone of the object and I want to gather it, I press "E" key to pick it up. The code on that object telling it to disappear from the world ( as I now have it in my inventory) also tells the Raw Image I have on the canvas to be enabled so that icon of the object visible on my HUD. Same for what weapon I am using, how many grenades I have etc. may not be the best way to go but it works for me. The images are all there just invisible until activated.
Answer by RunRockRise · May 03, 2020 at 07:30 PM
Hey, you could do something like this. Just drag an Image from you're assets to the "my_image" field, or set it via code. Hope this leads you in the right direction. And dont forget to add the right tag to your gameObjects and replace it in the script.
public Texture2D my_image;
public List<GameObject> my_gameobjects = new List<GameObject>();
private void Start()
{
for (int i = 0; i < my_gameobjects.Count; i++)
{
if (my_gameobjects[i].tag == "place_tag_here")
{
my_gameobjects[i].GetComponent<MeshRenderer>().material.mainTexture = my_image;
}
}
}
Your answer
Follow this Question
Related Questions
How to select an Game Object from an Item List 0 Answers
How to make a or array of GUI.Button's? 1 Answer
How do I stop new elements in a list/array from inheriting values from the last element? 1 Answer
How to make a Text scroll alongside the Text of an InputField? 1 Answer
Instantiate a prefab from jsonarray? 1 Answer