Code works in one place but not another?

I am working on a puzzle game, I implemented a pushblock puzzle and got everything working, but I decided to go back and add another kind of block that can’t be used to finish the puzzle, but can be used to help the other block get to the end of the puzzle by filling in holes in the ground, both blocks can fill in the hole though, I coppied my code from the puzzle end script to use as a base for the hole filling script, but it for some reason won’t work, here is both scripts for the main block in one block of code,

using UnityEngine;

public class PushPuzzleScript : MonoBehaviour
{
    public Transform bs;
    Rigidbody2D m_Rigidbody;
    public Transform pbs;
    public Transform hole;
    bool inHole = false;
  void Start()
  {
    m_Rigidbody = GetComponent<Rigidbody2D>();
  }
  void Update()
  {
    if (transform.position != bs.position)
    {
      if (Input.GetKey("r"))
      {
        transform.position = pbs.position;
        if (inHole == true)
        {
          m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
        }
      }
    }
  }
   void OnCollisionEnter2D(Collision2D collisionInfo)
   {
    if (collisionInfo.collider.tag == "Block Switch")
    {
        transform.position = bs.position;
        m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
    }
    if (collisionInfo.collider.tag == "Hole")
    {
        transform.position = hole.position;
        m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
        inHole = true;
        Debug.Log("The Push Block Fell In The Hole!");
    }
   }
}

I am trying to figure out why this code isn’t working, the top if statement in the on collision enter 2D works perfectly fine, however, the second one doesn’t, the code for the second block type has the same problem and can be seen here

using UnityEngine;

public class PushCrate : MonoBehaviour
{
    Rigidbody2D m_Rigidbody;
    public Transform hole;
    public Transform pcs;
    public Transform pb;
    public Transform bs;
    bool inHole = false;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        m_Rigidbody = GetComponent<Rigidbody2D>();
    }
    void Update()
  {
    if (pb.position != bs.position)
    {
      if (Input.GetKey("r"))
      {
        transform.position = pcs.position;
        if (inHole == true)
        {
            m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
        }
      }
    }
  }
    void OnCollisionEnter2D(Collision2D collisionInfo)
   {
    if (collisionInfo.collider.tag == "Hole")
    {
        transform.position = hole.position;
        m_Rigidbody.constraints = RigidbodyConstraints2D.FreezeAll;
        inHole = true;
    }
   }
}

I have also checked to see if it is something to do with unity itself, but that does not appear to be the case, the hole and the end pad for the puzzle have the same exact settings on everything, the only difference is the sprite and the tag itself as well as the layer. All help is apreciated!

Check that there was no confusion in this configuration:

Doc:Unity - Manual: Layer-based collision detection

Thank you, realized the confusion, had 2 very similarly named layers but with very different interactions.

1 Like

Who is still teaching that? Whatever learning source you’re using, please switch to something else. :wink:

The problem with string parameters in Input is that this literally requires you to emit that specific key from your keyboard. Meaning this is culture specific.

Image you were to make a shooter with “WASD” controls and use the above to check for the actual key presses. Along comes a frustrated french gamer and leaves a bad review. Why? Because that’s his keyboard:

Use KeyCode.W rather than a string. This will respond to the physical W key, which is the third key from the left in the fourth row from the bottom on any keyboard even for the frenchies (they have to press Z).