Changing a Sprite when Collision occurs


I’ve been trying to figure out how I would do a code that would be for my University project on a 2D Sidescroller Spaceship game.

The way this works is when the spaceship crashes in to a collider, I want the sprite to change to a different one which will be the same sprite, just I added a darker effect to it in Photoshop, and as it hits the collider of three times, eventually the GameObject will destroy and the level will fade out and restart.

Would it be best to do this on JS or C#? Can anyone get me started? :slight_smile:

In general the answer is you create the new object in the scene, copy the transform matrix from the old one, and delete the old one.

Alternately, you could avoid the transform copy by making the object have an empty parent object and just replace the children.

As for your second question, if this is going to be anything more then a quick small demo you should use C#. Javascript (really Unityscript, which is a close cousin of Javascript) can seem easier at first but any real game project quickly grows out into a level of complexity Unityscript can’t manage.

Check this out, I’ve created a tutorial for this :slight_smile: Unity 2D Tutorial [E01] : Swap GameObjects Sprite when they Collide - YouTube