1

I want to save the object which triggered as a variable and afterwards destroy it by pushing a key but I couldn't figure out how to save it as a variable.

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject)
         _canHit = true;
}

Edit: (Added the whole script to make it more understandable)

    //GameObject variable
    public GameObject collidedWith;

    //store the collided GameObject
    private void OnTriggerEnter(Collider other)
    {
        collidedWith = other.gameObject;
    }

    private bool _canHit;
    //if can hit true make it false and do the task
    private void Update()
    {
        if (_canHit)
        {
            if (Input.GetKeyDown(KeyCode.A) && collidedWith != null)
            {
                Destroy(collidedWith);
                _canHit = false;
                Debug.Log("Left");
            }
        }  
    }
    //set _canHit true if object enters trigger
    private void OnTriggerEnter(Collider other)
    {
        if (other.attachedRigidbody)
            _canHit = true;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject)
            trigObj = other.gameObject()
    }
 
    //set _canHit false if object enters trigger
    private void OnTriggerExit(Collider other)
    {
        if (other.attachedRigidbody)
            _canHit = false;
    }

2 Answers 2

2

You can make a variable of type GameObject to store the collided GameObject:

public GameObject collidedWith;//GameObject variable
private void OnTriggerEnter(Collider other)
{
    collidedWith = other.gameObject;//store the collided GameObject
}
private void Update(){
    //if the specified key is pressed and there is a collided GameObject, destroy the collided GameObject
    if(Input.GetKeyDown(keycodeForDestroying) && collidedWith != null){
        Destroy(collidedWith);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I added, it gives compiler error, I also editted the question you can check the script if u want to, ty for your response
@jetrldz please copy-paste the compiler error message and tell us what line of code the line number of the error refers to
Type 'collisiondetectorleft' already defines a member called 'OnTriggerEnter' with the same parameter types you can find it by ctrl +f "//set _canHit true if object enters trigger" under that line
okay I just fixed it thank you a lot
@jetrldz no problem, I have also added explanations for your compiler errors in another answer to this question
0

For compile errors:

//set _canHit true if object enters trigger
private void OnTriggerEnter(Collider other)
{
    if (other.attachedRigidbody)
        _canHit = true;
}

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject)
        trigObj = other.gameObject()
}

//set _canHit false if object enters trigger
private void OnTriggerExit(Collider other)
{
    if (other.attachedRigidbody)
        _canHit = false;
}

other is a Collider. other.attachedRigidbody is a Rigidbody, not a bool. If statements require a bool, which is a statement that is either true or false. You probably meant to check if there is an attached rigidbody, you can do this with other.attachedRigidbody != null. As the documentation for Collider says, attachedRigidbody

Returns null if the collider is attached to no rigidbody.

Another issue is in these lines:

if (other.gameObject)
        trigObj = other.gameObject()

similar to the attachedRigidbody, other.gameObject is not a bool. Use other.gameObject != null. In this case, I think this check is unnecessary, because if you collide with something, it must have a GameObject. Also, other.gameObject is a variable/property, not a method. Remove the () at the end of the line, they should only be there if it was a method.

Finally, you defined two OnTriggerEnter functions. You can put the code from the bottom one in the top one and delete the bottom one.

//set _canHit true if object enters trigger
private void OnTriggerEnter(Collider other)
{
    if (other.attachedRigidbody){
        _canHit = true;
    }
    trigObj = other.gameObject;
}

//set _canHit false if object enters trigger
private void OnTriggerExit(Collider other)
{
    if (other.attachedRigidbody != null)
        _canHit = false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.