Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by andrewwebber25 · Jul 13, 2017 at 04:55 PM · collisioncollider2d-platformerswappingis trigger

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
     }
 }

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrewwebber25 · Jul 13, 2017 at 08:46 PM 0
Share

@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!

avatar image Vicarian andrewwebber25 · Jul 13, 2017 at 09:08 PM 1
Share

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.

avatar image andrewwebber25 Vicarian · Jul 14, 2017 at 02:36 PM 0
Share

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!

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity2D side collision detection 1 Answer

How to prevent non-collider-objects from touching each other? 1 Answer

How to detect a collision between the character and the bottom of a cube? 1 Answer

I am trying to create all elements in the scene from one script at run time. How do I detect collisions between the enemy and the coin without having to create another script for each item? 0 Answers

How can I destroy an object if it touches NOTHING 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges