Search Unity

Can't Call OnTriggerEnter(Collider other)

Discussion in 'Editor & General Support' started by Mr_AgentFox, Jul 9, 2019.

  1. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    Hello! I am making a grenade pickup system and I can't seem to call the "OnTriggerEnter(Collider other)" method when I reference it inside the update method it shows the red underline under it. Any suggestions?

    Code:
    using UnityEngine;
    public class GrenadePickup : MonoBehaviour
    {
    public GrenadeThrower grenadeThrower;


    // Start is called before the first frame update
    void Start()
    {
    }


    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.P))
    {
    OnTriggerEnter();
    }
    }


    public void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.tag == "Grenade")
    {
    Debug.Log("GrenadePickUp");
    Destroy(gameObject);
    grenadeThrower.grenadeAmount += 1f;
    }
    }
    }
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    OnTriggerEnter takes a collider as an argument. You are trying to call it with no arguments.

    Are you doing this to test the function, or do you want to push P to pick up the grenade, or something?
     
  3. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59

    I want to press P so you pick up the grenade. Do you know how?
     
  4. You really need to go to the http://learn.unity.com and go through the beginner tutorial series. You're lacking absolutely basic concepts.

    In a nutshell:
    - put trigger collider on the objects you want to pick up
    - when the player character enters in the trigger show a UI message to "press <whateverbutton> to pick up"
    - when the player leaves the collider hide the message
    - when the player is in the trigger and they hit the <whateverbutton> then:
    --- destroy the object on the ground
    --- add the proper entry in the inventory you're making

    If you need help to build the inventory, you can head to either the learn section, they have a tutorial on it or more beginner friendly tutorials are on Youtube, I mean a lot. Just search and pick whichever is sympathetic.