- Home /
Collider Is Touching Layers is not working...
I know this is probably a simple answer, but I am very new to Unity and I can't seem to figure out what I'm doing wrong. I was following a 2D platformer tutorial series (https://www.youtube.com/watch?v=rMr1sHQ0_bc&list=PLpj8TZGNIBNy51EtRuyix-NYGmcfkNAuH) that showed a simple way to detect if the player is on the ground using this in a script:
if (Input.GetButtonDown("Jump") && coll.IsTouchingLayers(ground))
{
Jump();
}
This worked completely fine at the time. However, when I started a new project, I tried using the same way to do jumping, but the console shows errors, saying that there are errors with the next private void.
![Error][2] [2]: /storage/temp/170512-screen-shot-2020-11-05-at-85028-pm.png
My coding for the if statement is exactly the same, and I have a Jump function. I am able to jump if I don't input anything into the parentheses in the IsTouchingLayers. I have the exact same layer in the newer project, #8 being named ground. I looked and looked to see what differences there were between the projects but I could not find what was causing the problem. I'll provide the two different scripts below... Thank you to anyone who helps Tutorial and New Scripts
Edit: I just realized that the private void errors were caused by me forgetting a curly bracket, but it still does not work, as it says "The name 'ground' does not exist in the current context." Sorry for the error, again I am new.
Answer by swanne · Nov 06, 2020 at 08:35 AM
Hi,
The error message you are getting which explains that 'ground' does not exist in the current context leads me to believe that you have not declared a variable of type Layer, called ground.
Share your code here and I'll have a look
Thank you, I see the error in my code (I provided it in a pdf but for some reason, it wasn't showing up when I clicked the link... I had to open it in a new window and then download it...) I did not realize I needed a Layer$$anonymous$$ask variable named ground. I did this in the code that I had written down from the tutorial. If you don't $$anonymous$$d me asking - what exactly is a Layer$$anonymous$$ask? Why isn't the variable type just named Layer? What does the "mask" part mean? Thank you for your help even if this just felt like common sense to you!
Hey @i_cant_draw
Great to hear that you've got it working now. A layer mask is used to help us make sense of the layer. Unity uses assigns bits to its layers - so it refers to them as 1,2,3,4,5 etc. To save us humans from having to remember which layer number 4 is, Unity uses Layer$$anonymous$$asks, which allows us to provide a readable textual description. So as an example, when we say check for layermask 'ground', unity translates that to 'layer 28'. Layer$$anonymous$$asks help us manage things easier.
Sorry for the late reply, but thank you for explaining! Understanding what I'm doing really helps me to program better. I understand now, thank you so much!
Your answer
Follow this Question
Related Questions
Linecast Detecting Initial Collision Correctly, Need it to Stay Until Line Doesn't Intersect 1 Answer
Unity 2D - Game Collision Bug Error 0 Answers
How to change collision for just part of an object? (similar to sprite mask) 0 Answers
OnCollisionEnter2D is not working when an object tagged "Enemy" is not present in the game. 0 Answers