How i can destroy instances when i go throght them with mouse?

I’m trying to make something like ninja fruit on 3d , objects instantiate without problem and get dstroyed after a some time. I would to destroy the instances when i go throght them with mouse like in ninja fruit.

Here is code for instantiate things that i have:

private void FixedUpdate()
{
float x = Random.Range(0, 100001);

if(x<1000)
{
Vector3 SpawnPosition = new Vector3(0, 0, 62);
SpawnPosition = this.transform.position + Random.onUnitSphere * RangoCreacion;
SpawnPosition = new Vector3(SpawnPosition.x, SpawnPosition.y, 0);
GameObject Copia = Instantiate(pieces[UnityEngine.Random.Range(0, 6)], SpawnPosition, Quaternion.identity);
Destroy(Copia, 20f);

}

}

You could cast a ray through your screen at the mouse position, and if it hits some object (with a specific tag), destroy it. Unity - Manual: Rays from the Camera

Depending on the game you could also use an actual gameobject, which follows the mouse, and work with colliders to destroy hit objects. Placing this object under the mouse will likely require the above aswell tho.

Also, a few notes on code quality. FixedUpdate, should only be used for physics. None of what you put in there has anything to do with physics. Your spawn rate is currently based on the physics update rate. While this is mostly a fixed rate, it can be changed. You usually do not want such factors to change unrelated gameplay elements. This gets worse after you move that code to Update, where it belongs. Instead build yourself a little timer, where you remember the time you last spawned an object and have some spawn rate (possibly randomly set after each spawn, to reflect what you do now). You then simply check these values each frame and only spawn your next thingy once the current time is larger than the old value plus the spawnrate/timer. Now it’s independant of framerates, or anything else.
Last but not least, you should really code in english, even if it’s not your native language. There is several reasons for this. For one, Unity and any other API you will ever use, will be in english. So the best you can do is create some mixed language abomination that’s by definition inconsistant and likely hard to read. More importantly however, variable names are important for understanding any non-trivial piece of code. Unless you can guarantee that you are the only person who has to read and understand that code, these variable names should be written in a language anybody can understand, namely by convention, english. And just in case that’s not obvious, as soon as you post code on a forum to ask for help, someone else has to read and understand that code to help you :slight_smile:

    //Controls the size of the collider
    [SerializeField] float colliderSizeX;
    [SerializeField] float colliderSizeY;
    [SerializeField] float colliderSizeZ;
    GameObject Copia;
    private void Update()
    {
        //Moved this code into update as Yoreki suggested. Not sure why you have the if statement, couldn't you just make the range between 0 and 1000? Apart from that haven't changed the spawning code at all.
        float x = Random.Range(0, 100001);

        if (x < 1000)
        {
            Vector3 SpawnPosition = new Vector3(0, 0, 62);
            SpawnPosition = this.transform.position + Random.onUnitSphere * RangoCreacion;
            SpawnPosition = new Vector3(SpawnPosition.x, SpawnPosition.y, 0);
            Copia = Instantiate(pieces[UnityEngine.Random.Range(0, 6)], SpawnPosition, Quaternion.identity);

            BoxCollider copiaCollider = Copia.AddComponent<BoxCollider>();
            copiaCollider.isTrigger = true;
            copiaCollider.size = new Vector3(colliderSizeX, colliderSizeY, colliderSizeZ);
            InstantiatedCollisionScript copiaScript = Copia.AddComponent<InstantiatedCollisionScript>();
            //Destroy(Copia, 20f); (removed this code, can add it back if you want)

        }


    }

That is all for that class. It might seem slightly complicated, but it’s pretty simple. It’s really just adding a collider and script onto Copia. (if copia already has a collider maybe remove it?).

public class InstantiatedCollisionScript : MonoBehaviour
{
    private void OnMouseEnter()
    {
        Destroy(this.gameObject);
    }
}

Then this script checks if the mouse is hovering over the collider. I checked this out in a blank unity3d project with a cube and it worked well then. hope this helps :slight_smile: