- Home /
How can we display an image when the player collide with an object ?
Hello everyone,
I'm working on 2D Unity game and I would like to know how we can display an image on my inventory when the player collide with an object. For exemple, if the player collide with a drumstick, the drumstick image appear on the inventory menu.
This my code for the moment :
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Inventaire : MonoBehaviour {
public Image image;
private void OnTriggerEnter2D(Collider2D other, string itemName)
{
if (other.CompareTag("Player"))
{
if (itemName == "Grenade")
//grenade is the name of the item
{
image.enabled = true;
}
}
}
}
Best Regards.
Yacine TAZDAIT.
Hello, I think there is method called OnTriggerEnter2D and it will take only one argument not two. you can check out here. https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html
if you want to know the collision object then you can do by tags,names etc. So method looks like this
private void OnTriggerEnter2D(Collider2D other){ //Check your tags names here. }
Your answer
Follow this Question
Related Questions
Changing array of display images with other set of images on user button press event 0 Answers
Problem with Scale in game 1 Answer
Add item to GUI inventory and drop him back in the scene with keys 1 Answer
Problem with the image in 2D game on Android,Problem with the image in 2D game on Android. 1 Answer
Best Image Formats, Sizes, and Resolutions for Unity Sprites? 2 Answers