- Home /
 
How to set a layer with code using collisions?
I am trying to make an object go up or down in the layers when my player enters an area, but it isn't working. I am creating a 2d game. I am using C#.
This is my code:
{
 void OnCollisionEnter (Collision col) 
 {
     if (col.gameObject.name == "Player") 
         {
             col.gameObject.layer = 4;    
         }
 }
 void OnCollisionExit (Collision col)
 {
     if (col.gameObject.name == "Player") 
         {
             col.gameObject.layer = 2;
         }
 }
 
               }
Answer by JedBeryll · Dec 11, 2015 at 02:13 PM
Unfortunately layers don't work like that, 2 or 4 won't tell the program that you want the 2nd or 4th layer in the list. You can either save tha layermasks to variables or you can use this function: Layermask.NameToLayer("layerName");
Your answer
 
             Follow this Question
Related Questions
Physics Collision Layer Programmatically. 0 Answers
make the layer "IgnoreRaycast" work with collisions. 0 Answers
Linecast between character and camera doesn't ignoring Characters collider. 2 Answers
Temporary disable collision, what's best performance-wise 1 Answer
how to avoid collision between first and third layer when second is between them? 2D 0 Answers