How to throw a ball

Very much a beginner.

Trying to figure out how to instantiate a sphere object upon mouse click and then delete it after a couple seconds.

I am unsure which object I should create the script on. I tried making it on the camera, because it will be thrown from the cameras perspective and because I wasn’t sure if I could delete an object using a script on itself. I used delete(gameObject) and it seems that it deletes the object the script is on? In this case, the camera.

When trying to delete the object itself, though, it looks like it can’t run the code a second time because I’m only referring to a single instance and now it’s gone.

So how do I refer to the object I just instantiated when trying to delete it? And where should I be making my script?

Many thanks!

You could create an empty GameObject where you put your script on which instantiates the spheres. There you can also control the individual objects as follows:

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

public class GameController : MonoBehaviour
{
    public GameObject sphere;
    public Vector3 spherePosition; //position where you want the sphere to spawn

    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            GameObject newSphere = Instantiate(sphere, spherePosition, Quaternion.identity);
            Destroy(newSphere, 10); // 10 seconds, change this to what you want


        }

    }
}

Thank you so much!

How would I change this if I wanted it to be destroyed when it hit something. I’m trying to simulate a ball of paint being thrown (not paintball, so I do need to show the ball flying through the air)? I know I’ll need to create a splatter effect, but how do I make the newSphere destroy on collision?

I’ve tried to test it hitting items tagged “ground” with this code, but I don’t have a full understanding of how collisionInfo works as it’s telling me it does not exist in the current context.

  • void Update()
  • {
    • if (Input.GetMouseButtonDown(0))
  • {
  • GameObject newSphere = Instantiate(sphere, spherePosition, Quaternion.identity);
  • if (collisionInfo.collider.tag = “Ground”)
  • {
  • Destroy(newSphere, 2);
  • }
  • }
  • }

The best way is to add this in a separate script on the sphere itself. Managing collision outside of the object is a bit more complicated. Something like this should work:

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

public class SphereManager : MonoBehaviour {

    // Use this for initialization
    void Start () {
      
    }
  
    // Update is called once per frame
    void Update () {
      
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.transform.gameObject.tag == "Ground")
        {
            Destroy(gameObject, 2); //Since this script is on the actual gameobject, gameObject is a reference to your sphere
        }
    }
}

As for throwing the paint, you’ll want a rigidbody on the sphere object that you instantiate.
Using the code provided before, get the component of the sphere you spawned (rigidbody) and apply the force or velocity that you want.

Also, make sure this object has a collider set to IsTrigger (for the above code to work, too, when it hits something).