Search Unity

Destroying Object on Key Press while Colliding with it.

Discussion in 'Scripting' started by brucecollinsstudio, Sep 19, 2020.

  1. brucecollinsstudio

    brucecollinsstudio

    Joined:
    Aug 1, 2020
    Posts:
    1
    New to Unity. I've googled, searched and changed my code multiple times for over a week now. It's such a simple task but i'm obviously missing something. I simply want to destroy an object while pressing the "F" key while either looking directly at it or at least in its box collider. Ray casting seemed a bit daunting to me being new so I decided to test out just a simple way of destroying the object while being in the box collider of the object and pressing "F". I can't even get that to work... Please see the below code and thank you for any assistance.

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



    public class PantyCollect : MonoBehaviour
    {
    void Update()


    {
    void OnCollisionStay(Collision other)
    {
    if (other.gameObject.tag == "Player" && Input.GetKey(KeyCode.F))
    {
    Destroy(other.gameObject);
    }

    }
    }
    }
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Try destroying it without the key press, see if everything else is setup correctly.
     
  3. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    Void On collision enter? Why not using that. Useful to debug and see where the problem is exactly. And link the item with the expector. this way it would save you a lot of time later on because you might need it later. instead of having to make billion new scripts (Making your game waayy too large)
     
  4. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    What do you mean bud? You're not making any sense.

    Also, CollisionEnter would not work in his situation. And input is in the Update cycle, not FixedUpdate. So you won't end up with accurate event fires.
     
  5. RubenVanOostveen

    RubenVanOostveen

    Joined:
    Jul 31, 2020
    Posts:
    91
    My bad, That means one thing.... i screwd up my game where I was working on for the last couple of months... Thanks brother!




    Sorry again :C