Show text on collision not working?

I’ve spent hours on this, but I just can’t figure out what I’m doing wrong.
I’d just like to display some text when the player collides with a sphere.
Here’s the code I have on the sphere:

public class CollisionText : MonoBehaviour
{
private Text myText;
void Start()
{
    myText.enabled = false;
}
// Assuming you're using a 2D platform
void OnCollisionEnter(Collision collision)
{
   if(collision.gameObject.tag == "Player")
    {

        myText.enabled = true;
    }
}
void OnCollisionExit2D(Collision2D collision)
{
   
    if(collision.gameObject.tag == "Player")
    {
        myText.enabled = false;
    }
}

    
}

Here’s my player code as well in case there’s something in there that messes with the sphere’s code:

public class PlayerController : MonoBehaviour
{
    [SerializeField] Transform playerCamera;
    [SerializeField] float mouseSensitivity = 3.5f;

    float cameraPitch = 0.0f;
    [SerializeField] bool lockCursor = true;
    [SerializeField] float walkSpeed = 6.0f;
    [SerializeField][Range(0.0f, 0.5f)] float smoothTime = 0.3f;
    [SerializeField][Range(0.0f, 0.5f)] float MousesmoothTime = 0.03f;

    [SerializeField] float gravity = -13.0f;

    CharacterController controller = null;

    Vector2 currentDir = Vector2.zero;
    Vector2 currentDirVelocity = Vector2.zero;

    Vector2 currentMouseDelta = Vector2.zero;
    Vector2 currentMouseDeltaVelocity = Vector2.zero;
    float velocityY = 0.0f;



    void Start()
    {
        controller = GetComponent<CharacterController>();
        if(lockCursor)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        MouseLook();
        playerMovement();
    }

    void MouseLook()
    {
        Vector2 targetmouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetmouseDelta, ref currentMouseDeltaVelocity, MousesmoothTime);


        cameraPitch -= targetmouseDelta.y * mouseSensitivity;
        cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
        playerCamera.localEulerAngles = Vector3.right * cameraPitch;

        transform.Rotate(Vector3.up * targetmouseDelta.x * mouseSensitivity);

    }

    void playerMovement()
    {
        Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        targetDir.Normalize();
       
        currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, smoothTime);
      

        Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;

        controller.Move(velocity * Time.deltaTime);

        if(controller.isGrounded)
        {
            velocityY = 0.0f;
           
        }

        velocityY += gravity + Time.deltaTime;


    }

   
}

I’d appreciate any help. Thanks!

You’re using the 3D variant of the OnCollisionEnter but the 2D variant for the OnCollisionExit.

pro tip - slap on some debug statements to see what’s going on, what’s getting called and in what order.

1 Like

Does the player have a collider? Also, collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.

1 Like

Then it would have worked on collision enter at least, no?

by the second script it looks like the game is 2D so it’ll only work on exit. that’s why he’s not seeing anything instead of it only turning on, you’re correct that if the game was 3D the 2D exit would be missed and the 3D enter would be caught.

Could be he just forgot to attach the first script where it’s need in the first place :wink:

:smile:

thanks guys haha. All I needed was to slap on a rigidbody, and I fixed that 2D CollisionEnter mistake. Thanks again, and have a happy new year!