Clone once a GameObject in OnEnable

I’m trying to instantiate a clone that follows a GameObject with an offset. As this GameObject is disabled and enabled in game I’m creating it in OnEnable like this:

void OnEnable() {
    CreateClone();
}

private void CreateClone() {
    GameObject clone = Instantiate(gameObject);
}

The problem is that this way it loops infinitely because clone will have the same script that will generate a new clone and so on… I tried disabling script and enabling after but it had no effect.

How can I get only one clone each time the object is enabled?

You could have a static variable that you set to true for example after you make your clone, then in OnEnable, if the value is true, don’t create a clone.

Not sure this is the best design pattern though honestly, but it will work.

Another way is to keep a list on another script or a count, and check that to see if you have 2 objects already, if not, don’t clone.

The basic idea, no matter what you choose to do is to track that you have created a clone and don’t create another if you have.

The static variable is a better solution for me, thanks for helping :slight_smile: