[SOLVED] Bool change not reconized in second script

Updated on April 16, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
31 on April 16, 2016

Hi everyone,

I have an issue with the following script:

 using UnityEngine;
 using System.Collections;
 public class AbilityTurretTouch : MonoBehaviour {
 public bool turretDestruction = false;
 void OnCollisionEnter2D(Collision2D col){
 if (col.gameObject.tag == "Player") {
 Destroy (this.gameObject);
 turretDestruction = true;
 }
 }
 }
 

Let’s call this script A, which is assigned to a prefab. I have a second script, which I’ll call script B, which is assigned to another gameobject in the same scene.
In script B, I try to access turretDestruction within the Update function, however it always returns “False”, even if the collision in script A occurs. Thus I checked script A and turretDestruction DOES return “True” when the collision occurs. Therefore I am not sure why this is not working in script B. I know that, as this prefab is destroyed, the bool immediately switches back to “False”, but I just need it to be “True” for one frame, that would theoretically be enough for the other script to detect it..

Anyone knows what I’m doing wrong? Thx in advance!

  • Liked by
  • Buldron
Reply
1 on April 16, 2016

Your description confused me but I think this might fit your needs.
When the turret health == 0, have a countdown of 0.2 or something (less is best) seconds countdown to 0 before having the turret destroy itself.
 
Maybe disable the object’s mesh renderer and spawn an explosion to hide the gameobject delete delay; just make sure that you do these steps before the countdown.
 
Let me know if this was sort of what you wanted.

Helpful
on April 16, 2016

Sorry, there is a slightly better explanation of what I am trying to do below in the reply to Job. Hopefully it clarifies what I’m trying to do a bit more..

Show more replies
  • Liked by
Reply
Cancel
11 on April 16, 2016

You could try switching:

Destroy (this.gameObject);
 
 turretDestruction = true;

to:
 
turretDestruction = true;
 
Destroy (this.gameObject);

Devoted
on April 16, 2016

Lol, why didn’t I think of something so simple.
Probably the reason all my scripts run like potatoes. 
 
Nice work dude! 🙂

Devoted
on April 16, 2016

i was just wondering if you destroy the gameObject it will destroy the boolean variable too and then i suspect , if you try getting reference to the boolean it might throw null ref. error

Helpful
on April 16, 2016

Hi again. Thx for all your suggestions, unfortunately I did try swapping these already, but it’s still not working.. Referring to what Zoelove says: I think the boolean does not get destroyed, but it jumps back to its original “False” state. This is what’s happening if I print it in fact.

Maybe this can clear some things.. this script (script A) is attached to an ability sprite gameobject which spawns onto the battlefield. When the player touches it, this ability disappears, but obviously I want to keep track that the player has achieved this ability. And my tracker is in fact this turretDestruction bool.

Devoted
on April 16, 2016

Give Script B it’s own bool (e.g. turretDestructionCheck)
Have Script B access Script A’s bool when it’s about to be destroyed.
Change Script B’s bool’s value to be the same as Script A’s (only do once, maybe have a bool switch  to make sure it is not called twice).
 
Then destroy Script A , that way Script B will retain Script A’s bool value.

Helpful
on April 16, 2016

Okay, seems to make sense! I’ll try doing this asap thx!

Helpful
on April 16, 2016

I’ve tried everything.. unfortunately nothing is working, it’s always switching back to “False”. Well in case you come up with any ideas on how to implement tht let me know, otherwise thank you all anyways for the help!

Helpful
on April 16, 2016

Okay.. now in script A I did this and it’s working!

void OnCollisionEnter2D(Collision2D col){
 if (col.gameObject.tag == "Player") {
 turretAbilitySpawn.GetComponent<AbilityTurretSpawn> ().turretAbilityDestructionCheckTwo = true;
 turretAbilityDestruction = true;
 Destroy (this.gameObject);
 }
 }
Helpful
on April 16, 2016

Very strange.. it does become “True” in the inspector, but it remains “False” when I print it during runtime!

Devoted
on April 16, 2016

Strange indeed.
Maybe you have the debug collapsed in the console so you cannot see the change?

Helpful
on April 16, 2016

Apparently not :/.. I’ll keep trying things out in the meanwhile

Show more replies
  • Liked by
Reply
Cancel
8 on April 16, 2016

You could try and create a Coroutine with WaitForSeconds as its yield wait like 1 second and after that destroy the gameobject

Devoted
on April 16, 2016

He already tried doing a timer.
The problem is that the value reverts back to false after it is deleted.
Apparently he can’t save the bools value in the other script to prevent this.
I don’t really get why but if you have any suggestions please share. 🙂

Devoted
on April 16, 2016

Well then , you should create a Game Manager which will manage the progress of player . You can save the variable in Game Manager , so even if any of script A / B gets destroyed you can still have the boolean variable in Game Manager . To access just get reference.

Devoted
on April 16, 2016

Any declared default boolean is false . Same to a int – 0 and float 0.0f and string – “”

Devoted
on April 16, 2016

:p You already known that right .

Helpful
on April 16, 2016

Mmn I will look into the Game Manager, never heard of tht before, thx! It sounds like it can really be the solution..

Helpful
on April 16, 2016

Hey guys, what Or Aviram suggested below is working apparently! Thx to all anyways, and I will still have to look into Game Manager, don’t know how tht works either..

Show more replies
  • Liked by
Reply
Cancel
7 on April 16, 2016

Pretty strange none tried what I am about to tell you, but ok…
Try making the bool static. That means all of the scripts that use that bool will use the same one.

Helpful
on April 16, 2016

Okay thx! Will try tht and see what comes out 🙂

Devoted
on April 16, 2016

I had same idea , but static remains static , And that why it is bad idea of instantiating more than one script having same static variable .

Helpful
on April 16, 2016

It is working apparently, thanks man!! I have to look into “static” variables etc. I didn’t know this, very helpful!!

Devoted
on April 16, 2016

Zoelove, I didn’t say it’s a bad idea, that’s actually what I told him to do. 😛
And Mito (I don’t really know how to call you), this ks because when you are making an instance, you are creating a new object of that type, and when you access it you are accessing this object’s variable, and not a general (static) one that is being shared through all of the scripts.

Devoted
on April 16, 2016

Nah just saying 😛 .

Helpful
on April 16, 2016

Ahhh okay, so as I understand it basically the “static” part makes this variable common for all my prefabs which have this script attached, and if one of these prefabs just gets destroyed, the bool information is not lost. While, in my previous case, I was losing the bool information since that bool was only assigned to that specific instance.. correct?

Devoted
on April 16, 2016

Indeed. 😉

Show more replies
  • Liked by
Reply
Cancel