How to limit GameObject replications

I’m trying to create a self replicating GameObject that will stop replicating itself once a certain number of replications has been reached (a random value that is defined in the initial GO script). I have 2 GOs/scripts… one that initiates the replication and one that handles each subsequent instantiation. I understand how to read variables from the initial GO script using “GameObject.GetComponent” to get certain needed variables for the next instantiation, but I don’t understand how to write back to the initial script to keep count of how many replications have been made. “GameObject.SendMessage” doesn’t seem to be the right way to go for this… any suggestions?

Perhaps I don’t understand what you are trying to do, but can’t you just:

using UnityEngine;

public class Duplicator : MonoBehaviour {
   public GameObject original;
   public int copies = 0;

   void OnStart() {
      for (int i = 0; i < copies; i++)
         Instantiate(original);
   }
}

Not sure if I understand you correclty,

var toWriteBack : name of original replicate script;

var count = 0;

then somewhere inside your function.
{

count ++; or whatever you wanted to write;
 
toWriteBack.theVariableOfOriginalReplicateScript = count;
}

I think what he means is that when you press a button, to instantiate a new gameObject, but if there are a certain number of gameObjects onscreen stop instantiating them. Like how the old megaman games only let you shoot three bullets at a time.

You could use a static variable to keep track of the GO count, and access it using ScriptName.varName for both writing and reading.

Thanks Ray (and all), that was it…I was overthinking just how easy it is to read/write between scripts :sweat_smile:

I do that all the time too! :smile: