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);
}
}
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.