0
\$\begingroup\$

I am just starting out with Unity following along with this video, and I am stuck at roughly 1 hour and 54 minutes in: https://www.youtube.com/watch?v=pwZpJzpE2lQ

It is worth noting that the video uses Unity 2019.4.4f1 whereas I am using Unity 2020.3.2f1.

My issue is that I cannot get my character to pick up coins and they are currently solid, uncollectable objects. The coins are all set to the Coin layer (layer 9) as per the video.

Here is my code (which I looked over a dozen times to see if there were any differences from the video's code and couldn't find any, hence my confusion). Does anyone know where I went wrong?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField] private Transform groundCheckTransform = null;
    [SerializeField] private LayerMask playerMask;

    private bool jumpKeyWasPressed;
    private float horizontalInput;
    private Rigidbody rigidbodyComponent;
    

    // Start is called before the first frame update
    void Start()
    {
        rigidbodyComponent = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        // Check if space key is pressed down
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jumpKeyWasPressed = true;
        }

        horizontalInput = Input.GetAxis("Horizontal");
    }

    // FixedUpdate is called once every physic update
    private void FixedUpdate()
    {
        rigidbodyComponent.velocity = new Vector3(horizontalInput, GetComponent<Rigidbody>().velocity.y, 0);

        if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0)
        {
            return;
        }

        if (jumpKeyWasPressed)
        {
            rigidbodyComponent.AddForce(Vector3.up * 7, ForceMode.VelocityChange);
            jumpKeyWasPressed = false;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 9)
        {
            Destroy(other.gameObject);
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Can you add a screenshot of you Coin Gameobject from the Inspector? \$\endgroup\$
    – Zibelas
    Commented Apr 2, 2021 at 8:52

2 Answers 2

2
\$\begingroup\$

It sounds like you didn't tick the "Is Trigger" checkbox on the coins' Collider component.

Colliders have two modes:

  • Solid (Is Trigger = false): the physics engine will try to stop and bounce-away movement that makes this collider intersect with another solid collider, generating an OnCollisionEnter event when they touch.

  • Trigger (Is Trigger = true): the physics engine treats this collider as a ghost that can pass through solid colliders with no interference, generating an OnTriggerEnter event instead.

If your coins do not have Is Trigger checked, then they'll be treated as solid obstacles and will not call your OnTriggerEnter method.

\$\endgroup\$
2
  • \$\begingroup\$ This was my first thought too. \$\endgroup\$
    – jhocking
    Commented Apr 2, 2021 at 13:44
  • \$\begingroup\$ Just did this and they are no longer solid. Thank you! \$\endgroup\$
    – eneuman96
    Commented Apr 2, 2021 at 21:46
0
\$\begingroup\$

I think the problem is with layer number. Try this

private void OnTriggerEnter(Collider other)
{
    // if you want to use tag 
    if (other.transform.CompareTag("ObjectCoinTag"))
    {
    Destroy(other.gameObject);
    }

    // Or, you want to use the name object
    if (other.transform.name == "ObjectCoinsName") {
    Destroy(other.gameObject);

    }
    
}

I would like to advice you to watch short tutorials like the one unity do on their website.

\$\endgroup\$
3
  • \$\begingroup\$ I just tried replacing the previous OnTriggerEnter code with that code and it had the same result of the coins just being solid objects that can't be collected. \$\endgroup\$
    – eneuman96
    Commented Apr 2, 2021 at 7:20
  • \$\begingroup\$ Did you change the "ObjectCoinsName" to object coin name? Is the trigger checked ? Look follow this good tutorial. Remember, we all started like you. \$\endgroup\$
    – user43474
    Commented Apr 2, 2021 at 8:02
  • \$\begingroup\$ This one worked for me. Thank you! \$\endgroup\$
    – eneuman96
    Commented Apr 2, 2021 at 21:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .