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 Deeeds · May 10, 2018 at 05:04 AM · collisionspriteswap

Swap object sprites on collision, how?

When two objects collide, I want to swap their sprites, but the below code doesn't work.

What am I doing wrong?

 public class SwapSprite : MonoBehaviour {
  
     private SpriteRenderer _mySpriteRenderer;
     private Sprite _otherSprite;
     private Sprite _mySprite;
    
     private void Start ()
     {
         _mySpriteRenderer = GetComponent<SpriteRenderer>();
     }
  
     private void OnCollisionEnter2D(Collision2D other)
     {
         _otherSprite = other.otherRigidbody.GetComponent<SpriteRenderer>().sprite;
     }
  
     private void OnCollisionExit2D(Collision2D other)
     {
         if (_otherSprite == null)
         {
             // do nothing
         }
         else
         {
             _mySpriteRenderer.sprite = _otherSprite;
         }
     }
 }


UPDATE:::

The following almost works well, except for a problem with sometimes all sprites becoming the same texture...

 public class SwapTexture : MonoBehaviour {
 
     private SpriteRenderer _mySpriteRenderer;
     private Sprite _otherSprite;
     private bool swapSprite = false;
 
     private void Start ()
     {
         _mySpriteRenderer = GetComponent<SpriteRenderer>();
     }
 
     private void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.CompareTag("Playa"))
         {
             swapSprite = true;
             _otherSprite = other.gameObject.GetComponent<SpriteRenderer>().sprite;
         }
     }
     private void OnCollisionExit2D(Collision2D other)
     {
         
         if (swapSprite)
         {
             swapSprite = false;
             _mySpriteRenderer.sprite = _otherSprite;
         }
     }
 }
Comment
Add comment · Show 13
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 Jaz30 · May 10, 2018 at 07:26 AM 0
Share

Update it again with this:

 Sprite _thisSprite;
 
      private void OnCollisionEnter2D(Collision2D other)
      {
          if (other.gameObject.CompareTag("Playa"))
          {
              swapSprite = true;
              _thisSprite = _mySpriteRenderer.sprite;
              _otherSprite = other.gameObject.GetComponent<SpriteRenderer>().sprite;
          }
      }
      private void OnCollisionExit2D(Collision2D other)
      {
          
          if (swapSprite)
          {
              swapSprite = false;
              _mySpriteRenderer.sprite = _otherSprite;
              other.gameObject.GetComponent<SpriteRenderer>().sprite = _thisSprite;
          }
      }

$$anonymous$$aybe this will work.

avatar image Deeeds Jaz30 · May 10, 2018 at 07:29 AM 0
Share

This won't work because both objects have this script, so this will just create a double swap, meaning no change.

avatar image Jaz30 · May 10, 2018 at 07:50 AM 0
Share

Update it again with this, make sure you change the NameOftheScript thing on my code. Just read it well:

 public static Sprite thisSprite;
 
      private void OnCollisionEnter2D(Collision2D other)
      {
          if (other.gameObject.CompareTag("Playa"))
          {
              swapSprite = true;
              _thisSprite = _mySpriteRenderer.sprite;
              _otherSprite = other.gameObject.GetComponent<NameOfYourScriptr>().thisSprite;
          }
      }
      private void OnCollisionExit2D(Collision2D other)
      {
          
          if (swapSprite)
          {
              swapSprite = false;
              _mySpriteRenderer.sprite = _otherSprite;
          }
      }

$$anonymous$$aybe this will work.

avatar image Deeeds Jaz30 · May 10, 2018 at 07:53 AM 0
Share

You never use _thisSprite, nor thisSprite.

So it's functionally the same as my update, so far as I can tell.

Perhaps I'm reading this wrong, or missing something.

Ooopss... no, it seems you're changing this to this, by calling it _otherSprite

avatar image Jaz30 Deeeds · May 10, 2018 at 07:55 AM 0
Share

Okay I'm wrong syntax sorry 'bout that:

  public static Sprite _thisSprite;
  
       private void OnCollisionEnter2D(Collision2D other)
       {
           if (other.gameObject.CompareTag("Playa"))
           {
               swapSprite = true;
               _thisSprite = _mySpriteRenderer.sprite;
               _otherSprite = other.gameObject.GetComponent<NameOfYourScriptr>()._thisSprite;
           }
       }
       private void OnCollisionExit2D(Collision2D other)
       {
           
           if (swapSprite)
           {
               swapSprite = false;
               _mySpriteRenderer.sprite = _otherSprite;
           }
       }
Show more comments
avatar image Jaz30 · May 14, 2018 at 02:01 AM 0
Share

Check this out, I've created a tutorial for this :) https://www.youtube.com/watch?v=ma5OBfPRxcg&list=PLiIYE_N6G$$anonymous$$rZgLF5fgPNxavmOr3t5RRkv

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jaz30 · May 10, 2018 at 05:58 AM

-First you need to create a Folder inside Asset Folder and name it "Resources"

-Inside Resource Folder create a folder again and name it "Sprites"

-Inside of the sprite folder, put the images/sprite you need for the collision, lets say those images named spr1 and spr2

-Here's the snippet code you can use:

 //Lets say we have two gameobjects, those two gameobjects sprite will swap when collide. Lets say those two gameobjects named go1 and go2
 
 SpriteRenderer mySpriteRenderer;
 
 void Start()
 {
     mySpriteRenderer = GetComponent<SpriteRenderer>();
 }
 
 void OnCollisionEnter2D(Collision2D col)
 {
     //Lets say this gameObject (go1) collided to go2
     //so the value of spriteName will be spr2, 'cause go2's spritename is spr2 right?
     string spriteName = col.gameObject.GetComponent<SpriteRenderer>().sprite.name;
 
     //The function I used here is to locate and get the sprite name from "spriteName" variable.
     //As you can here below, it says (Sprite)Resource.Load("Sprites/" + spriteName), if you got confuse where the "Sprites/ + spriteName" came from
     //it goes like this  (Sprite)Resource.Load("FolderName/" + spriteName (the variable we declared))
     mySpriteRenderer.sprite = (Sprite)Resource.Load("Sprites/" + spriteName) as Sprite;
 }

I hope you got an idea with this codes. I'm not saying that this is totally correct, but I think you'll get an idea with this.

Comment
Add comment · Show 11 · 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 Jaz30 · May 10, 2018 at 06:06 AM 0
Share

You can also try this, I don't think this would work.

  SpriteRenderer mySpriteRenderer;
  
  void Start()
  {
      mySpriteRenderer = GetComponent<SpriteRenderer>();
  }
  
  void OnCollisionEnter2D(Collision2D col)
  {
     //simply swap their sprites
     // put your current sprite into temp var
      Sprite mySprite = mySpriteRenderer.sprite;
     // then get the sprite of the gameobject you collided
      mySpriteRenderer.sprite =  col.gameObject.GetComponent<SpriteRenderer>().sprite;
     // then change the sprite of collided gameobject using the sprite from temp variable
      col.gameObject.GetComponent<SpriteRenderer>().sprite = mySprite;
  }


Just try it. :)

avatar image Deeeds Jaz30 · May 10, 2018 at 06:15 AM 0
Share

Look at my code. I'm already creating a temporary variable to store the OTHER's Sprite, during the collision, and then waiting until collisionExit to apply that Sprite, so there's time for the OTHER sprite to do exactly the same thing.

avatar image Deeeds · May 10, 2018 at 06:15 AM 0
Share

The sprites already exist, in the game world, on the objects. There should be no need to access any folder, assets or texture files.

avatar image Jaz30 Deeeds · May 10, 2018 at 06:37 AM 0
Share

$$anonymous$$aybe because of this:

  _otherSprite = other.otherRigidbody.GetComponent<SpriteRenderer>().sprite;

try this:

  _otherSprite = other.gameObject.GetComponent<SpriteRenderer>().sprite;
avatar image Deeeds Jaz30 · May 10, 2018 at 06:39 AM 0
Share

I've tried this, it doesn't work either. It does make one significant difference, sometimes the objects revert to having no sprite, ie drawing a white box ins$$anonymous$$d of the sprite they hit.

Show more comments
avatar image
0

Answer by Deeeds · May 10, 2018 at 11:07 AM

Thanks to @Jaz30

Here's a solution that does it all:

 using UnityEngine;
 
 public class SwapTexture : MonoBehaviour {
 
     private SpriteRenderer _mySpriteRenderer;
     private Sprite temp;
 
     private void Start ()
     {
         _mySpriteRenderer = GetComponent<SpriteRenderer>();
     }
 
     private void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.CompareTag("Playa"))
         {
             tag = "Gangsta";
             temp = other.gameObject.GetComponent<SpriteRenderer>().sprite;
             other.gameObject.GetComponent<SpriteRenderer>().sprite = _mySpriteRenderer.sprite;
             _mySpriteRenderer.sprite = temp;            
         }        
     }
     private void OnCollisionExit2D(Collision2D other)
     {
         tag = "Playa";
     }
 }
 

Damn, it feels good to be a Gangsta! https://www.youtube.com/watch?v=I1L8l3LrzLA

Comment
Add comment · Show 1 · 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 Jaz30 · May 14, 2018 at 02:03 AM 0
Share

Check this out, I've created a tutorial for this :) https://www.youtube.com/watch?v=ma5OBfPRxcg&list=PLiIYE_N6G$$anonymous$$rZgLF5fgPNxavmOr3t5RRkv

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

152 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 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

Problems with 2D Collision 3 Answers

2D animation collision 1 Answer

Sprite collision with Tilemap 2 Answers

How do you create complex collision geometry to match 2D sprites? 2 Answers

SpriteRenderer animation generics? 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