- Home /
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
import System.Collections.Generic;
var enemies:GameObject[] = new GameObject [1];
private var tempGO:GameObject[] = new GameObject[3];
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 = new List.<GameObject>();
private var tempEnemyScript = new List.<Enemy> ();
//private var tempEnemyScript : Enemy[] = new Enemy[4];
function Start () {
playerY = playerpos.position.y;
thisTransform = transform;
spawnenemytype(enemies[1],firstWave);
StartCoroutine("CheckForDeleted");
}
function CheckForDeleted(){
yield WaitForSeconds(1);
//tempEnemyScript = new Enemy[spawnedEnemies.Count];
for (var i:int = 0; i<spawnedEnemies.Count; i++){
//tempEnemyScript = new Enemy[spawnedEnemies.Count];
tempEnemyScript.Add( spawnedEnemies[i].GetComponent("Enemy"));
if (tempEnemyScript[i].deleted)
{
Debug.Log("deleted");
enemycounter--;
}
else {
Debug.Log("Active");
}
}
}
function Update ()
{
offset = new Vector3(0,Random.Range(minset,offsetY),0);
///Debug
//Debug.Log("did it work "+ tempEnemyScript.Count);
Debug.Log("did it work "+ spawnedEnemies.Count);
//Debug.Log("did it work "+ tempGO.Length);
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);
//CheckForDeleted();
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++)
tempGO[i] = Instantiate(enemytype,spawnLocation,Quaternion.identity);
spawnedEnemies.Add(tempGO[i]);
Debug.Log("how many enemies "+spawnedEnemies.Count);
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);
}
}
Answer by Wolfram · May 05, 2014 at 06:46 PM
You destroy the GameObject to which Enemy.js is attached, which will also destroy that Enemy.js instance. So accessing "deleted" afterwards will not only not work, it is an illegal action.
Instead, just ignore that "deleted" bool: if you destroy Enemy.js, your variable countScript will simply become null; use that instead of the bool.
Also note you are accessing enemy[1] (=the second array entry) instead of enemy[0], not sure if that is your intention.
but i thought if i put it above the destroy line it will still trigger the boolean,i have revised the code if you look i have now added to store each instantiated enemy which will then pass into my tempEnemyScript list so i can then call each of the enemys created scripts but for some reason tempEnemyScript has 1 element inside i used tempEnemyScript.Count to check
@JJB653 you did not understand the concept I was trying to explain. "Destroy(this.gameObject)" destroys the gameObject the script is attached to, including all components and all children, and therefore also including the script instance itself. Trying to access myScriptInstance.disabled from that point on is illegal, as myScriptInstance no longer exists, and you are accessing a Null reference.
As a result, tempGO[x] of the detroyed object will also become null, as will the matching entry in the spawnedEnemies List. In Line 45 you then add that null entry (or even worse: calling GetComponent() on that null entry) to your tempEnemyScript List. All that accomplishes is adding a "null" List element to your List, which is valid, and which is the reason tempEnemyScript.Count is 1. But it doesn't help you finding anything out about your enemy object.
Ins$$anonymous$$d, as I already explained, simply use the fact that tempGO[x]/spawnedEnemies[x] becomes null as an indicator that a specific instance has been destroyed.
You also ignored my last remark. In line 31 (and at several other places), you are accessing "enemies[1]", which is the SECOND entry of your "enemies" array, not the first one. $$anonymous$$ost likely you mean "enemies[0]".
i kind of understand what your saying but if the object hasnt been destroyed yet then why would it be null? and the reason im accessing enemies[1] is because i put the enemy object in that element in the inspector
If it hasn't been destroyed yet, the gameObject will still exist. So checking this fact is exactly what you want. For example, ins$$anonymous$$d of your loop starting in line 43, do something like:
enemycounter=0;
for (var i:int = 0; i<spawnedEnemies.Count; i++){
if(spawnedEnemies[i]!=null)
enemycounter++;
}
// cleanup dead waves
if(enemycounter==0)
spawnedEnemies.Clear();
A different approach would be for your Enemy.js script to call a method in your Spawn script to decrease the enemycounter if Enemy.js decides to destroy itself.
yes that can work but i ran into a problem with trying to access the spawn script form the enemy script as the enemy isn't in the hierarchy so i cant insert the script in the inspector
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Trigger script does not work 1 Answer
Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers
How to tell if two blocks are right next to each other?(2D) 1 Answer
Proper way to Pool Object - Can't acces due to protection level 1 Answer