- Home /
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;
}
}
}
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.
This won't work because both objects have this script, so this will just create a double swap, meaning no change.
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.
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
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;
}
}
Check this out, I've created a tutorial for this :) https://www.youtube.com/watch?v=ma5OBfPRxcg&list=PLiIYE_N6G$$anonymous$$rZgLF5fgPNxavmOr3t5RRkv
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.
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. :)
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.
The sprites already exist, in the game world, on the objects. There should be no need to access any folder, assets or texture files.
$$anonymous$$aybe because of this:
_otherSprite = other.otherRigidbody.GetComponent<SpriteRenderer>().sprite;
try this:
_otherSprite = other.gameObject.GetComponent<SpriteRenderer>().sprite;
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.
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
Check this out, I've created a tutorial for this :) https://www.youtube.com/watch?v=ma5OBfPRxcg&list=PLiIYE_N6G$$anonymous$$rZgLF5fgPNxavmOr3t5RRkv
Your answer
Follow this Question
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