Boolean not changing

Hey im trying to make the enemycounter inside the enemyspawn script decrement after the deleted bool is true which is found in the enemy script but for some reason it always equals false i dont understand why. here’s the code

Spawn.js

var playerpos:Transform;
var playerY:float;
var enemycounter:int;
var offset:Vector3;
var enemyTypeAllowed:int;
var newPosition : Vector2;
var spawn_position:Vector3;
var timer:float;
var isSpawning:boolean =false;
private var firstWave:int = 4;
 
 
private var thisTransform : Transform;
private var offsetY:float = 50;
private var minset:float = 20;
 
 
 
function Start () {
 
//countScript = enemies.GetComponents("Enemy"); 
countScript = enemies[1].GetComponent("Enemy");
if (countScript.deleted==true)
{
Debug.Log("did it work " );
//enemycounter--;
}
 
playerY = playerpos.position.y;
thisTransform = transform;
spawnenemytype(enemies[1],firstWave);
 
 
 
}
 
function Update () 
{
 
 
offset = new Vector3(0,Random.Range(minset,offsetY),0);
Debug.Log("did it work " + countScript.deleted );
playerY = playerpos.position.y;
timer +=Time.deltaTime;
 
newPosition = Random.insideUnitCircle * 5;
 
if (enemycounter == 0  !isSpawning)
//for ( var i:int = 0; i >=firstWave; i++)
{
    spawnenemytype(enemies[1],firstWave);
    timer =0;
 
}
 
}
 
 
 
function spawnenemytype (enemytype:GameObject, amount:int)
 
{
 
var spawnLocation:Vector3;
//var count:int = 0;
var temp:int;
isSpawning = true;
//offset = new Vector3(0,Random.Range(minset,offsetY),0);
 
//while (count < amount)
for ( var i:int = 0; i < amount; i++)
{
    yield WaitForSeconds(1);
 
    temp = Random.Range(1,5);
    //temp = 1;
    if (temp == 1)
    {
 
      spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
      Debug.Log("Creating enemy number1: " );
      //temp = Random.Range(1,5);
    }
 
    if (temp == 2)
    {
 
      spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
      Debug.Log("Creating enemy number2: " );
      //temp = Random.Range(1,5);
    }
 
 
    if (temp == 3)
    {  
 
      spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
      Debug.Log("Creating enemy number3: " );
      //temp = Random.Range(1,5);
    }
 
    if (temp == 4)
    {
 
      spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
      Debug.Log("Creating enemy number4: " );
      //temp = Random.Range(1,5);
    }
 
 //for ( var i:int = 0; i <=amount: i++)
 Instantiate(enemytype,spawnLocation,Quaternion.identity);
 
 enemycounter++;
 Debug.Log("Creating enemy number4: " + enemycounter );
 //Instantiate(enemies[0],new Vector3(newPosition.x,playerY+offset.y,0),transform.rotation);
}
 
isSpawning = false;
 
}

Enemy.js

private var hasAppeared:boolean = false;

//private var delete:boolean = false;
 
var deleted:boolean =true;
function Start () {
 
 
 
Debug.Log("deleted is false");
 
 
 
}
 
function Update () {
 
 
if (renderer.isVisible)
 
    {
 
    hasAppeared = true;
    Debug.Log("I can see a cube!!");
 
    }
 
if (hasAppeared  !renderer.isVisible)
{
 Debug.Log("I cant see a cube!!");
 deleted = true;
hasAppeared = false;
Destroy(this.gameObject);
}
 
}
 
/*function OnBecameVisible () 
{
Debug.Log("I can see a cube!!");
 
 if (!renderer.isVisible){
 Debug.Log("I cant see a cube!!");
 
       //Destroy(this.gameObject);
 
}
}
*/
 
 
function OnTriggerEnter2D(theCollision : Collider2D){
 if(theCollision.gameObject.name == "Player")
{
  deleted = true;
  Destroy(this.gameObject);
 
  }
 
 
 
 
  }

Please help its urgent still been trying but cant get it to work :frowning:

can anybody help me

Just realized why it wont work and my suggestion wont work either as you need to store instantiated enemies to access there variables.

Just revised it but i don’t code in UnityScript and i have not tested it and just wrote this on Notepad.

    var playerpos:Transform;
    var playerY:float;
    var enemycounter:int;
    var offset:Vector3;
    var enemyTypeAllowed:int;
    var newPosition : Vector2;
    var spawn_position:Vector3;
    var timer:float;
    var isSpawning:boolean =false;
    private var firstWave:int = 4;
     
     
    private var thisTransform : Transform;
    private var offsetY:float = 50;
    private var minset:float = 20;
     
    private var spawnedEnemies : List.<GameObject>;
	private var tempEnemyScript : Enemy;
     
    function Start () {
     
    /*
	This will never work.
	countScript = enemies.GetComponents("Enemy");
    countScript = enemies[1].GetComponent("Enemy");
    if (countScript.deleted==true)
    {
    Debug.Log("did it work " );
    //enemycounter--;
    }
	*/
     
    playerY = playerpos.position.y;
    thisTransform = transform;
    spawnenemytype(enemies[1],firstWave);
    
	
     StartCoroutine("CheckForDeleted");
     
    }
     
	 function CheckForDeleted(){
		yield WaitForSeconds(1f); //Check every 1 seconds
		for (i = 0; i < spawnedEnemies.length; i++){
			tempEnemyScript = spawnedEnemies.GetComponent(Enemy);
				if (tempEnemyScript.deleted) {
					Debug.Log("Deleted!");
				}else{
					Debug.Log("Active");
				}
		}
	}
	
    function Update ()
    {
     
     
    offset = new Vector3(0,Random.Range(minset,offsetY),0);
    Debug.Log("did it work " + countScript.deleted );
    playerY = playerpos.position.y;
    timer +=Time.deltaTime;
     
    newPosition = Random.insideUnitCircle * 5;
     
    if (enemycounter == 0  !isSpawning)
    //for ( var i:int = 0; i >=firstWave; i++)
    {
        spawnenemytype(enemies[1],firstWave);
        timer =0;
     
    }
     
    }
     
     
     
    function spawnenemytype (enemytype:GameObject, amount:int)
     
    {
     
    var spawnLocation:Vector3;
    //var count:int = 0;
    var temp:int;
    isSpawning = true;
    //offset = new Vector3(0,Random.Range(minset,offsetY),0);
     
    //while (count < amount)
    for ( var i:int = 0; i < amount; i++)
    {
        yield WaitForSeconds(1);
     
        temp = Random.Range(1,5);
        //temp = 1;
        if (temp == 1)
        {
     
          spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
          Debug.Log("Creating enemy number1: " );
          //temp = Random.Range(1,5);
        }
     
        if (temp == 2)
        {
     
          spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
          Debug.Log("Creating enemy number2: " );
          //temp = Random.Range(1,5);
        }
     
     
        if (temp == 3)
        {  
     
          spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
          Debug.Log("Creating enemy number3: " );
          //temp = Random.Range(1,5);
        }
     
        if (temp == 4)
        {
     
          spawnLocation = new Vector3(newPosition.x,playerY+offset.y,0);
          Debug.Log("Creating enemy number4: " );
          //temp = Random.Range(1,5);
        }
     
     //for ( var i:int = 0; i <=amount: i++)
	 //Changed this to stored spawned enemies
     var tempGO : GameObject = Instantiate(enemytype,spawnLocation,Quaternion.identity);
	 //Register the spawned enemy in the list.
	 spawnedEnemies.Add(tempGO);
     
     enemycounter++;
     Debug.Log("Creating enemy number4: " + enemycounter );
     //Instantiate(enemies[0],new Vector3(newPosition.x,playerY+offset.y,0),transform.rotation);
    }
     
    isSpawning = false;
     
    }

I have commented the changes.

alright thanks ill look up how to store instantiated objects into variables

I just updated my post. I would suggest formatting your code better and use capitalization properly for variables and functions as it looks really messy. Easy to read code will be easier to debug.

o thanks ill test it out and i know sorry just been trying different things was gonna clean up the code after i got this working

thanks meancoder helped me alot, ive changed your code and used the generic list and also used count instead of length which isnt available, but the tempscript only stores the last enemy so im going to add a dynamic array for the scripts to store each enemy script when spawned think that should do the trick thanks again

i cant seem to access the array of scripts i did

for (var i:int = 0; i<spawnedEnemies.Count; i++){
	//tempEnemyScript = new Enemy[spawnedEnemies.Count];
	tempEnemyScript[i] = spawnedEnemies[i].GetComponent("Enemy");
	if (tempEnemyScript[i].deleted)
	{
		Debug.Log("deleted");
	}
	else { 
		Debug.Log("Active");  
		}

	}

then in the update i put

Debug.Log("did it work " +tempEnemyScript[1].deleted);

but a error comes up saying object reference not set to an instance of an object what have i done wrong?

The first element in an array is always 0 not 1. so if you only have 1 element in the array it would be tempEnemyScript[0]

but if spawnedEnemies has 4 objects in its list, the for loop iterates till it hits 4 which should make tempEnemyScript eventually tempEnemyScript[3] isnt that right?

Nevermind, found the answer to my question.

function OnTriggerEnter2D(theCollision : Collider2D){
 if(theCollision.gameObject.name == "Player")
{
  deleted = true;
  Destroy(this.gameObject);
}

You set your bool then delete the object… where do you expect it to read the bool from with the object destroyed?

Why not before you destroy the enemy… just get rid of the bool and have it decrement your counter before destroying itself?

// new var
var spawner : Spawn; // my spawner has a spawn script... thats what we want

// inside your start function
spawner = FindObjectOfType(typeof(Spawn)); // assuming we have 1 of these objects

if(!spawner) // if the spawner is null or not found
{
 // better do something here to make sure stuff doesn't break
}




// in your enemy script
spawner.enemycounter--; // get rid of bool and just subtract this dead enemy
///then destroy or whatever

thanks for the reply your right thats what i ended up doing, i used singleton to get the enemycounter, they say FindObjectOfType is very expensive

I said that to get you going… and only to call it once. Singleton is better or even just a public variable populated in the inspector. Any way you go, its best to have that enemy add or remove itself from any counter otherwise that logic gets convoluted and trick to figure out.

I had this in the example:

if(!spawner) // if the spawner is null or not found
{
 // better do something here to make sure stuff doesn't break

//This is usually where I put the FindByType style solution
// that way If i forgot to populate it... it finds it
}