0
\$\begingroup\$

I have a PickupTarget object in front of my FirstPersonCharacter which is used for 'holding' picked up object. Somehow, it doesn't work; when I click on the object (Axe) it just stays on the ground.

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

public class PickUp : MonoBehaviour {

    public Transform target;

    void Update () {

    }

    void onMouseDown() {
        this.transform.position = target.position;
        this.transform.parent = GameObject.Find ("FirstPersonCharacter").transform;
    }

    void onMouseUp() {
        this.transform.parent = null;
    }
}

I have added PickupTarget as a target of script in Axe object:

target

This is the structure of my Axe object.

Axe object

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

onMouseDown() is just a random function. You aren't calling it from anywhere in your code and Unity won't call it because it's not the MonoBehaviour event method.

You want OnMouseDown().

Same goes for your onMouseUp(): OnMouseUp()

Capitalization matters.

Also make sure that the script the GameObject is attached to has its own Collider component as well.

\$\endgroup\$
6
  • \$\begingroup\$ +1 Also as a note, I believe in the latest Unity downloads, functions are required to be capitalised at the start. \$\endgroup\$
    – cmprogram
    Jan 19, 2018 at 9:10
  • \$\begingroup\$ @cmprogram I am not so sure about that. I've got this function void callbackMethod(Message msg) in a 2017.2 project right now. \$\endgroup\$ Jan 19, 2018 at 16:50
  • \$\begingroup\$ Edit: It works with the cube (GameObject > 3D Object > Cube), but still doesn't work with the Axe object. \$\endgroup\$ Jan 19, 2018 at 19:43
  • 1
    \$\begingroup\$ @Nikola Does your axe have a collider? (Attached to the same GO as the script). \$\endgroup\$ Jan 19, 2018 at 19:44
  • \$\begingroup\$ @Draco18s I have added Box Collider as a component of Axe object and it works now, thanks! \$\endgroup\$ Jan 19, 2018 at 19:47

You must log in to answer this question.

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