Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JJB653 · May 04, 2014 at 10:56 PM · gameobjectboolean

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);
   
   }
  
  
  
      
   }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JJB653 · May 06, 2014 at 02:00 PM 0
Share

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

avatar image Wolfram · May 06, 2014 at 02:23 PM 0
Share

@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]".

avatar image JJB653 · May 06, 2014 at 02:28 PM 0
Share

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

avatar image Wolfram · May 06, 2014 at 02:44 PM 0
Share

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.

avatar image JJB653 · May 06, 2014 at 03:16 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges