How do I add a Rigid body and a box collider component to a Texture2D?

I am making a snake game. I'm basing it on a basic tutorial game, which does no collision detection, wall checking or different levels.

All snake head, piece, food, even the background is made of Texture2D. I want the head of the snake to detects 2D collisions with them, but Rect.contains isn't working. I'd prefer to detect collisions by onTriggerEnter() for which I need to add BoxCollider to my snakeHead.

• I don't understand the question. Are you asking how to check if two rectangles collide? – Anko Mar 26 '14 at 10:49
• no, not at all. I am asking can we add physics2D components to the Texture2D object? or If something else that I can do for it? – Adnan Nazir Mar 26 '14 at 11:26
• Is there a reason you can't add BoxCollider2D? You have an entity, you have a texture attached to it, and you know the other thing you need to add. Why not add it? – Seth Battin Mar 28 '14 at 19:41
• How do you draw your Texture2D? If you use sprites then it's an object which ALSO can have a BoxCollider component. – okutane Jan 21 '15 at 10:11
• Well, What are you people thinking?? Well, the answer I was trying to figure out was indeed because of my unfamiliarity with Unity. I was making Texture with GUI.DrawTexture in OnGUI() Method. Now the problem has been solved. As I didn't had any mentor so just learnt things by just posting stuff and getting answers thanks allot. :) – Adnan Nazir Jan 21 '15 at 14:19

To get onTriggerEnter events, you need to set the Is Trigger flag to true.

• @gamenewdev I have a feeling that did not answer your question.. – pek Mar 27 '14 at 14:54
• Yeah U have the right feeling :( – Adnan Nazir Mar 28 '14 at 6:12
• @gamenewdev, rather than insincerely thanking people who try to answer based on the limited information available from you, you should improve your question to allow better answers to be posted. You might start by explaining why this doesn't answer your question. – Seth Battin Mar 28 '14 at 19:37
• Ok @SethBattin I'll definitely try next time. I am not a native English speaker, also I am totally a bigginer in unity. That's why I was unable to tell you what I am seeking. – Adnan Nazir Apr 1 '14 at 13:41
• You wouldn't be adding the box collider etc to a texture2d... You'd be adding it to a GameObject that has a texture2d... You are creating a snake game that grows when you eat food? When your snake eats good you procedurally create a new body node and you want to know how to add a collider and rigidbody to that?? Is that what your actual question is? – Savlon Apr 26 '14 at 3:49

There should be an object that holds your texture and to that object you could add a collider.

If your game is 2D, you can use Rigidbody2D and BoxCollider2D. It will give you a performance boost, and will ignore Z position differences.

To get OnTriggerEnter or OnTriggerEnter2D, you need to check the "Is Trigger" checkbox in collider settings. Please note that doing so will remove your ability to make rigidbody respond collisions.

As you have already added "2D collisions", then you can't add "BoxCollider" because it is a 3D object. You have to add the components of the same type. Meaning, if you are going to use the 3D object like "BoxCollider", then you have to add a 3D collision not 2D collision. That means you have to add "Shpare collider" instead of 2D collider.