0

Hey I am trying to make my sprite button go from green to red and backwards when my player collides with it. I have a boxcollider2d as is trigger, and a script

script : https://gyazo.com/ec64a2bca5b23526c6949bf18cb50a0d

I think this is some of it but I dont get anything at all when colliding with the button can anyone enlighten me on this problem would be very appreciated.

Thanks in advance

sisva
  • 41
  • 6

1 Answers1

3

It is important to distinguish the difference between Colliders and Triggers.

Colliders are used by the Physics system in order to make it so object do not penetrate or overlap each other. Triggers on the other hand are made so that you can check for overlapping areas, an example is a Ring in Sonic. Sonic collects the rings but they do not stop him from passing, think of these as triggers. If they were colliders sonic would jump into them and bounce off.

In Unity the process for creating a trigger is the same for creating a collider, with 1 additional step, and that is marking a check box.

As such it is also important to know which functions you should use in each case.

OnCollision... For Colliders, (Or Colliders not marked as triggers)

OnTrigger... for Triggers, (Marking a collider in unity as a trigger makes it a trigger.)

To just add a bit more detail, It is also important to use the correct collider types for the correct dimensions of the game, 2d Colliders/Triggers do not interact with 3d Colliders/Triggers. More so physics components: 2d Rigidbodies do not works with 3d colliders, and 3d rigidbodies do not work with 2d colliders/triggers.

As such there are functions for each 2d, 3d, collision, and trigger.

Another important factor is to ensure if you want to use these functions atleast one of the objects have to have a rigidbody of the correct type(2d or 3d).

As such the problem you were running into was using OnCollisionEnter2D for a "collider" that was marked as a trigger, in this case OnCollisionEnter2D is not called, but OnTriggerEnter2D is called.

AresCaelum
  • 1,558
  • 1
  • 13
  • 18
  • Yes i found out still new to programming and really love that all of you are so helpful. It is still a very new thing for me here at uni and we are trying to make a game for our project. Have only programmed for 1.5months so far so a lot to learn still :d – sisva Nov 08 '18 at 14:07
  • @Simon no problem at all good luck in your learning adventures, I would suggest when you have time to check out some of Unity's tutorials, here is a link that goes over Colliders and Triggers: https://unity3d.com/learn/tutorials/s/physics – AresCaelum Nov 08 '18 at 14:09
  • @Simon not sure I follow you, how do you have an array in c# you don't know how many? arrays in c# have a getLength method. – AresCaelum Nov 08 '18 at 14:17
  • @Simon Are you allowed to use a foreach loop? https://stackoverflow.com/questions/398982/how-do-foreach-loops-work-in-c – AresCaelum Nov 08 '18 at 14:19
  • 1
    @Simon Well, I would suggest possibly using a foreach loop... Give it a try, and if you have issues post a new question with the issues you ran into. Either Me or one of the numerous of other helpful people here will be able to help ya out. – AresCaelum Nov 08 '18 at 14:23