Home > Software engineering >  Unity C# How to save object as variable
Unity C# How to save object as variable

Time:10-21

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;
    }

CodePudding user response:

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);
    }
}

CodePudding user response:

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;
}
  • Related