How to create only one Clone with Instantiate in Start()

Hello, how do I create only one clone with Instantiate?
Always when I hit start ther are continuesly spawning apples on my screen. But I only want one clone.
This is my code:

public class applespawn : MonoBehaviour
{
    public Transform prefab;
    // Start is called before the first frame update
    void Start()
    {
        float posx = Random.Range(-8.0f, 6.0f);
        float posy = Random.Range(-4.0f, 4.0f);
        this.transform.position = new Vector2(posx, posy);

        float lposx = Random.Range(-8.0f, 6.0f);
        float lposy = Random.Range(-4.0f, 4.0f);
        Instantiate(prefab, new Vector3(lposx, lposy, 0), Quaternion.identity);
    }
}

Does your apple prefab have this “applespawn” script on it?

If so, every time you spawn an apple, that apple is going to spawn a new apple, because of this script, and so on forever.

Take this script off your apple prefab, and put it on a different object.

2 Likes

Oh… Thank you, im new to Unity xD

You could also track how many instances of this class exist using a static variable. Increase in Awake for every new one, in OnDestroy you decrease. Then you can decide based on the current number if you should instantiate more. But for you use case, @PraetorBlue is giving you better advice.