Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 pottyscotty · Apr 05, 2014 at 10:50 AM · collider 2dchanging shader

Change a Sprite when clicked?

Been stuck on this for ages! Im wondering how to make a 2D Sprite change texture when it is clicked (Inside it's collider). Then I need to to change back to it's old texture when you click elsewhere. It's probably quite simple but I can't seem to work it out, any help will be appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by slek120 · Apr 05, 2014 at 12:29 PM

 using UnityEngine;
 using System.Collections;
 
 public class SpriteSwitch : MonoBehaviour
 {
     public Sprite oldSprite;
     public Sprite newSprite;
     SpriteRenderer spriteRenderer;
 
     void Start ()
     {
         spriteRenderer = GetComponent<SpriteRenderer> ();
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetMouseButtonDown (0)) {
             Vector3 mousePos = Input.mousePosition + Vector3.forward * Vector3.Distance (transform.position, Camera.main.transform.position);
             Vector3 worldPos = Camera.main.ScreenToWorldPoint (mousePos);
             Vector2 testPos = new Vector2 (worldPos.x, worldPos.y);
 
             if (collider2D.OverlapPoint (testPos)) {
                 spriteRenderer.sprite = newSprite;
             } else {
                 spriteRenderer.sprite = oldSprite;
             }
         }
     }
 }

One caveat, when the sprite changes, the collider still stays the same.

Comment
Add comment · Show 6 · 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 pottyscotty · Apr 05, 2014 at 12:52 PM 0
Share

I get this error: NullReferenceException: Object reference not set to an instance of an object SpriteSwitch.Update () (at Assets/Scripts/$$anonymous$$isc/SpriteSwitch.cs:19)

alt text

problemwithscript.png (172.4 kB)
avatar image slek120 · Apr 05, 2014 at 12:58 PM 0
Share

hmm. That's funny. $$anonymous$$aybe it's the Camera.main. Do you have a camera that is tagged as "$$anonymous$$ainCamera"?

avatar image pottyscotty · Apr 05, 2014 at 01:11 PM 0
Share

I have 2 cameras at the moment. One is my main one, the other is the $$anonymous$$imap. The $$anonymous$$imap camera is just called $$anonymous$$inimap, my main one is just called Camera. Below is the properties of my main camera. alt text

cameraprops.png (178.3 kB)
avatar image slek120 · Apr 05, 2014 at 01:38 PM 0
Share

At the top of the inspector, there is a "Tag" set to "Untagged". Change that to "$$anonymous$$ainCamera". Either that, or in the script, add "public Camera mainCamera" and link the camera in the inspector. Then replace "Camera.main" with "mainCamera".

avatar image pottyscotty · Apr 05, 2014 at 01:45 PM 0
Share

Thank you so much, it works perfectly! Exactly what I wanted :)

Show more comments

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

23 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

Related Questions

Looking for: Simple pathfinding in 2D Tower Defense game with no objects to collide with? 1 Answer

Collision2D compare the y axis of the object colliding with the object being collided with 0 Answers

How to add ParticleSystem in point where CollisionTrigger2D touch another? 1 Answer

Cannot move Polygon Collider 2D vertices in one project 0 Answers

Rigidbody2D Player collider seems larger than displayed 0 Answers


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