Show a photo when hitting a collider
Hi, I work on a simple 2D jump and run game, SuperMario style. Whenever the player hits a coin, I want to: - pause the game - Show a splash screen with a text and image - remove the coin
I am quite far on the first two, but I would like to be able to show a different text and image for each coin. So I would like to have a public field in unity where i can drag in the photo object and another to type in the photo text, for each of the coins. Could you please help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Show_Photo : MonoBehaviour
{
public GameObject Overlay;
public Image Photo;
public TextEditor PhotoText;
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Object that entered the trigger : " + other);
if (other.CompareTag("Player"))
{
// Update Photo
// Show overlay
Overlay.SetActive(true);
// Update Headline
// Pause Game
Time.timeScale = 0;
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
// Remove coin
// resume game
Overlay.SetActive(false);
Time.timeScale = 1;
}
}
}
Answer by artischo · Jun 16, 2019 at 10:54 AM
I think I solved it. Maybe not perfect, but it works. Would be great if I wouldn't have to assign the container objects in the inspector window, but could hard-code them into the code...
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Show_Photo : MonoBehaviour
{
public GameObject Overlay;
public Image PhotoField;
public Text PhotoTextField;
public string PhotoText;
public Sprite Photo;
bool PhotoRing = false;
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Object that entered the trigger : " + other);
if (other.CompareTag("Player"))
{
// Pause Game
Time.timeScale = 0;
PhotoRing = true;
// Update Photo
PhotoField.sprite = Photo;
// Update Headline
PhotoTextField.text = PhotoText;
// Show overlay
Overlay.SetActive(true);
}
}
private void Update()
{
if (PhotoRing == true && Input.GetKeyDown(KeyCode.Space))
{
Overlay.SetActive(false);
PhotoRing = false;
Time.timeScale = 1;
Destroy(this.gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
How can I detect the collision of a gameObject which is at a high speed? 1 Answer
OnTriggerEnter getting called more than once 0 Answers
When two different 3d objects touch each other I want one of them to turn green, how do I do this? 0 Answers
How to random place sprites but make sure they aren't touching? 1 Answer
Controlling Collisions of Rotated Parent's child-object 0 Answers