Home > Back-end >  I cant delete an instantiated object in unity c# using Delete()
I cant delete an instantiated object in unity c# using Delete()

Time:06-22

So im coding a grappler from my game (code is from a tutorial with minor tweaks), and im trying to get an prefab instantiated on the top of the grapple that shows the top. its currently just an empty object with a sprite renderer. The grappling is working i just need to figure out how to delete the object when im not pressing the grapple button.

Code:

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

public class grapple : MonoBehaviour
{
    public Camera mainCamera;
    public LineRenderer _lineRenderer;
    public DistanceJoint2D _distanceJoint;
    public Transform target;


    // Start is called before the first frame update
    void Start()
    {
        _distanceJoint.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Vector2 mousePos = (Vector2)mainCamera.ScreenToWorldPoint(Input.mousePosition);
            _lineRenderer.SetPosition(0, mousePos);
            _lineRenderer.SetPosition(1, transform.position);
            _distanceJoint.connectedAnchor = mousePos;
            _distanceJoint.enabled = true;
            _lineRenderer.enabled = true;
            Instantiate(target, mousePos, Quaternion.identity);
            //if (Input.GetKey("Space"))
            //{

            //transform.position = Vector2.MoveTowards(transform.position, target.position, 4 * Time.deltaTime);
            //}

        }
        else if (Input.GetMouseButtonUp(1))
        {
            _distanceJoint.enabled = false;
            _lineRenderer.enabled = false;
            Destroy(target);
        }
        if (_distanceJoint.enabled)
        {
            _lineRenderer.SetPosition(1, transform.position);
        }
    }
}

CodePudding user response:

Save instantiated object to a class member variable and use it for destroying later

    GameObject _instantiatedObj;

    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Vector2 mousePos = (Vector2)mainCamera.ScreenToWorldPoint(Input.mousePosition);
            _lineRenderer.SetPosition(0, mousePos);
            _lineRenderer.SetPosition(1, transform.position);
            _distanceJoint.connectedAnchor = mousePos;
            _distanceJoint.enabled = true;
            _lineRenderer.enabled = true;
            _instantiatedObj = Instantiate(target, mousePos, Quaternion.identity);
            //if (Input.GetKey("Space"))
            //{

            //transform.position = Vector2.MoveTowards(transform.position, target.position, 4 * Time.deltaTime);
            //}

        }
        else if (Input.GetMouseButtonUp(1))
        {
            _distanceJoint.enabled = false;
            _lineRenderer.enabled = false;
            Destroy(_instantiatedObj);
        }
        if (_distanceJoint.enabled)
        {
            _lineRenderer.SetPosition(1, transform.position);
        }
    }

  • Related