Destroy a gameObject through another script

So i’ve been stuck at this for an hour now which is strange because I didnt think it would be this hard.

So im having my ball script spawn an arrow before the player launched the ball the goal is that the player will be able to choose where the ball is shot depending on the rotation of the arrow but i can’t seem to destroy the arrow after creating it without issues.

Code
 public void Launch_Ball()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Vector3 spawnPoint = new Vector3(this.transform.position.x, -10.5f, 10f);
           

            lauchingBall = true;
            Instantiate(arrow, spawnPoint , arrow.transform.rotation);

             
        }

        else if (Input.GetMouseButtonUp(1))
        {
            lauchingBall = false;
            LaunchedBall = true;
            Destroy(arrow.gameObject); //  Written like this the newly made arrow wont be destroyed.
                                                         
            GetComponent<Rigidbody>().velocity = new Vector3(BallVelocityX, 0f, BallVelocityZ);
        }


    
    }

If I assign the arrow gamebject to the newly instantiated arrow, it will destroy it but then i cant spawn it again.

ex: arrow = Instantiate(arrow, spawnPoint , arrow.transform.rotation);

Rather than Instantiate() and Destroy() over and over again, what if you just toggle the component’s active state on and off like this:

private void Start()
{
    arrow.SetActive(false); // Make sure the arrow is off when the scene is loaded
}

public void Launch_Ball()
{
    if (Input.GetMouseButtonDown(1))
    {
        Vector3 spawnPoint = new Vector3(this.transform.position.x, -10.5f, 10f);

        lauchingBall = true;

        arrow.SetActive(true); // Turn the arrow on
    }
    else if (Input.GetMouseButtonUp(1))
    {
        lauchingBall = false;
        LaunchedBall = true;

        arrow.SetActive(false); // Turn the arrow off

        GetComponent<Rigidbody>().velocity = new Vector3(BallVelocityX, 0f, BallVelocityZ);
    }
}

Then you can just leave the GameObject in the scene and keep the reference to the arrow in the scene. You can even leave it active in the editor so you can see it since it will automatically be turned off when Start() is called.

Otherwise, you might try declaring a separate variable like GameObject spawnedArrow so that you’re leaving your reference arrow alone.

I had tried using SetActive and that had its own issues for me, the first spawn wouldn’t show up but the second would. Assigning the new variable works similar to the example , it will spawn and destroy properly the first time but then it won’t spawn again because I destroyed it.

Previously I tried using GameObject.FindWithTag but again it would destroy once and I couldn’t spawn it again. For some reason though Destroy(GameObject.Find("Arrow(Clone)")); works properly.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms