- Home /
How to switch one image with another when walked into?
Hey guys so im trying to do an image swap in my 2D Platformer and its more difficult than I thought it would be for me. Lets say for example I have to sprites, one image is a blue circle, the other is a red circle. I have the blue circle in my game with a collider on it. How can I make it that when my player (tagged as Player and has a collider) touches the blue circle, it turns into the red circle image? My thoughts are that if I set the collider on the blue circle to Is Trigger, I can tell it to image swap when my Player tagged "Player" collides with it. Any thoughts on how I would set this up? I know this is a pretty loaded question but my code has not worked so far :/ Any help would be awesome!
Answer by Vicarian · Jul 13, 2017 at 06:22 PM
Attach this script to the gameobject with the blue circle and link the two sprite fields via the inspector.
using UnityEngine;
[RequireComponent(typeof(Image))] // Prevent race condition on Image
public class ExampleClass : MonoBehaviour {
[SerializeField] Sprite m_initialSprite, m_collidedSprite;
Image m_image;
void Awake() {
m_image = GetComponent<Image>();
m_image.sprite = m_initialSprite;
}
// Collision example
void OnCollisionEnter(Collision collision) {
Debug.Log("Collision Enter fired from: " + name);
if (collision.collider.tag == "Player")
m_image.sprite = m_collidedSprite;
}
// If you want the collided sprite to persist when the player leaves, remove this
void OnCollisionExit(Collision collision) {
Debug.Log("Collision Exit fired from: " + name);
if (collision.collider.tag == "Player")
m_image.sprite = m_initialSprite;
}
// Trigger Example
void OnTriggerEnter(Collider other) {
Debug.Log("Trigger Enter fired from: " + name);
if (other.tag == "Player")
m_image.sprite = m_collidedSprite;
}
// If you want the collided sprite to persist when the player leaves, remove this
void OnTriggerExit(Collider other) {
Debug.Log("Trigger Exit fired from: " + name);
if (other.tag == "Player")
m_image.sprite = m_initialSprite;
}
}
@Vicarian Thanks for the help but it still isnt working right. I got the code to debug with no errors at all but when I attach my two images to it in inspector, the player still walks right through the collider. He still has the tag Player and Is Trigger is set up. Ive even tried deleting some of your examples in the code just to see if i can get it to work on the $$anonymous$$imum and still nothing. Any ideas? This code looks awesome and I really want to get it running!
Does one of the collision objects have a Rigidbody component attached? If you don't want to mess with a player controller by adding a rigidbody to it, you may add the rigidbody to the trigger zone.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html Note the statement requiring that one of the colliding objects have a Rigidbody.
Hey @Vicarian, $$anonymous$$y player does have a rigidbody attached to it. I even put one on the blue image and it didnt make a difference. I have used On Trigger commands previously on this scene where I have an enemy with a collider (Is Triggered) and it interacts with my player properly. Any ideas are greatly appreciated!
Answer by cstooch · Jul 13, 2017 at 06:19 PM
Could have sworn I just answered this question already (maybe someone else also asked this recently), but..
yes, make your circle's collider a trigger. OnTriggerEnter, you call your function that sets the image to red. OnTriggerExit, you call your function that sets the image to blue. And as you mentioned, ensure that the object colliding is tagged as player.
If you aren't getting that to work, post your code attempt.