Keep constant number of prefabs

This should be fairly trivial and I don’t know what’s wrong with my script. I seem to have trouble understanding how to send messages.My gamemanager script keeps track of the number of prefabs and has two functions that are called when a prefab is created/destroyed. I checked that both CountPlatformsUp() and CountPlatformsDown() are called, but my “platforms” variable doesn’t keep track and once destroyed platforms are not spawned in Update().

Why?

From the prefab:

function Start () {
    gameManagerObject.GetComponent(_gameManagerScript).CountPlatformsUp();
}

function OnDisable() {
    gameManagerObject.GetComponent(_gameManagerScript).CountPlatformsDown();
}

The gamemanager:

var platformPrefab : GameObject;
var spawnAreaObject : GameObject;

private var spawnAreaTransform;
private var platforms : int = 0;

function Start () {
	spawnAreaTransform  = spawnAreaObject.GetComponent(Transform);
}

function Update () {
	if (platforms <= 20) {
		SpawnPlatform();
	}
}

function CountPlatformsUp () {
	platforms = platforms + 1;	
}

function CountPlatformsDown () {
	platforms = platforms - 1;
}

function SpawnPlatform() {
	var targetPos : Vector3 = Vector3(Random.value * spawnAreaTransform.position.x, Random.value * spawnAreaTransform.position.y, 1.434096);
	Instantiate(platformPrefab, targetPos, Quaternion.identity);
	//platforms = platforms + 1;
}

Is the game manager script called “_gamemanagerScript.js”? You should pass the script name without quotes or extension to GetComponent - if the name is “GameManagerScript.js”, use GetComponent(GetManagerScript).

Anyway, there’s a good method to keep track of the current population of platforms: create an empty object, call it “Platforms”, reset its position and rotation and assign the script below to it. Whenever a new platform is created, it’s childed to the Platforms object - this way you can just check childCount to know how many platforms are alive (Unity always updates the childCount when a child is added or destroyed).

var platformPrefab : GameObject;
var spawnAreaObject : GameObject;

private var spawnAreaTransform;

function Start () {
    spawnAreaTransform  = spawnAreaObject.transform; // get transform directly
}

function Update () {
    if (transform.childCount <= 20){
       SpawnPlatform();
    }
}

function SpawnPlatform() {
    var targetPos : Vector3 = Vector3(Random.value * spawnAreaTransform.position.x, Random.value * spawnAreaTransform.position.y, 1.434096);
    var platf: GameObject = Instantiate(platformPrefab, targetPos, Quaternion.identity);
    platf.transform.parent = transform; // child the new platform to this object
}