Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

platform changes to the wrong layer

Discussion in '2D' started by MasonEngland, Apr 29, 2022.

  1. MasonEngland

    MasonEngland

    Joined:
    Feb 17, 2022
    Posts:
    2
    Okay so I have a script attached to the child of a platform. this child is an empty object with a box collider 2D set as trigger.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlatformLimiter : MonoBehaviour
    6. {
    7.     public GameObject platform;
    8.     private bool layerCheck;
    9.     public LayerMask firstLayer;
    10.     public LayerMask secondLayer;
    11.  
    12.     private void Update()
    13.     {
    14.         if (layerCheck)
    15.         {
    16.             platform.layer = firstLayer;
    17.         }
    18.         else
    19.         {
    20.             platform.layer = secondLayer;
    21.         }
    22.     }
    23.     private void OnTriggerEnter2D(Collider2D collision)
    24.     {
    25.         StartCoroutine(layerChange());
    26.     }
    27.  
    28.     IEnumerator layerChange()
    29.     {
    30.         layerCheck = true;
    31.  
    32.         yield return new WaitForSeconds(1);
    33.  
    34.         layerCheck = false;
    35.     }
    36. }
    I am new to unity but my goal was to make it so that I would have to be on the other side of the platform effector before I could jump again because before it would launch the player anytime they crossed a platform effector. firstLayer is set to "default" and secondLayer is set to "ground"(the layer my player can jump on) but for some reason when I enter the trigger it changes the platform layer to "Player". why is that? how do I fix it?
    btw I tried a different file as well that didn't have the if statements but essentially did the same thing, the added if statements in the update function were just an attempt to correct it but I got the same result either way.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Keep in mind the above might be working properly and then some other script might be setting it back.

    Here's some steps to help track down what might be happening:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.
     
  3. MasonEngland

    MasonEngland

    Joined:
    Feb 17, 2022
    Posts:
    2
    thank you for all the debugging tips. I will certainly use them in the future. however, I currently feel like the worlds biggest dumbass because I found the solution to my problem by trying a different approach. all I had to do was check if the rigid body's y velocity was less than 0.1...that was it. all I had to do was add one more jump condition and it would have saved me the 2 weeks of attempted debugging to find this out. I made THREE 60-100 line scripts to try and solve a problem that took not even 1 line of code to fix...so yeah, I just feel stupid rn but I guess that's how coding goes.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    ^ ^ ^ ^ This all day long.

    Our team once took a week to figure out why a single é was coming out as a messed-up character once it went through Facebook.

    Let me tell you, that é had gone through hell and back to arrive at our final destination... no wonder it was messed up!