boolean variable not staying as false and resetting back to true

I have a raycast and if I shoot at the enemy collider and the bullet tags the collider AND the variable ‘a’ is true (which is initialized at the top) I play a death animation and then I set the ‘a’ boolean to false because if I shoot at the same position again the enemy repeats the death animation when he is already dead which is why I added a boolean to try and prevent that

but every time I click to shoot it seems that my boolean is resetting back to true, why is this happening? ): Am I calling the a boolean at the wrong place?

Here is my script

var BulletSpeed = 50; 
var speed : float = 5.0;
var movement = true; 
var a : boolean = true;


function Start () {

}



function Update () {



        transform.Translate(Vector3.forward * Time.deltaTime * BulletSpeed);
        var hit : RaycastHit;
        
        
        if (Physics.Raycast(transform.position, transform.forward, hit))
        {      

             if (hit.collider.gameObject.tag == "z"   a == true) {
             GameObject.Find("soldier2").animation.Play("soldierDieBack"); 
             movement = false;  
             }
             
             if(movement == false){
              GameObject.Find("soldier2").GetComponent(Move_Soldier).enabled = false;
              a = false;
              Debug.Log("a should be false" + a);
             
             }
   
            }             

}

My last line in the code or the debug seems to read out “a should be false false” which is what I want because at the top I’m saying IF a == TRUE and bullet hits tag then play death animation.

So the false is resetting back to true because even after the enemy dies it plays the death animation.

Guessing the bullet has the above script applied,
every new instance of that bullet will spawn with a = true.

1 Like

Yes the bullet has the above script applied, is there any way to come about this or any other techniques? so that once the die animation plays that’s it

I tried using this code below and disabling the gameobjects die animation so it doesn’t play over again but it didn’t work

GameObject.Find(“soldier2hit”).GetComponent(soldier2dies).enabled = false;

You could make a boolean on the enemy soldier to determine if he is dead or alive, then refer to that through the bullet script with an if statement so the death animation only plays if the boolean is true

1 Like

Thanks I guess that’s the only way, i tried doing it but got some errors msut be tored or something

I was also thinking is there a way I could just disable the animation of an object?

so something like this

GameObject.Find(“soldier2hit”).GetComponent(soldier2dies).enabled = false;

but instead of getting a script, can I name an animation and disable it so it’s never played again?

I’m not sure about disabling, its not something I’ve tried, i expect someone else will know though

The bool to show if he is dead or not is the way i would go. Let me know if you carry on with that and need a hand, not on here for much longer but will be on tomorrow

Hey James thanks, I will work on that tomorrow will hope to let you know what happens whether I solve it or not but this programming is killing me!

1 Like

Ok James, I made a script for my enemy soldier and called it deathscript

I must call 2 variables from 2 different scripts in each script, am I right?

Because in the bulletscript I am saying if bullet tags enemy collider play death animation so in there I need to make a new boolean and say
var deathactivate = true;

so then I call this in the mew script I made in the enemy and say

if deathactivate == true then play the death animation there???

and then make it false?

So I tried it but I’m getting this error

NullReferenceException: Object reference not set to an instance of an object
DeathScript.Update () (at Assets/Scripts/DeathScript.js:11)

Here is part of my bulletscript

var BulletSpeed = 50; 
var speed : float = 5.0;
var movement = true; 
var c = false; 

function Update () {



        transform.Translate(Vector3.forward * Time.deltaTime * BulletSpeed);
        var hit : RaycastHit;
        
        
     
             
              
            
        if (Physics.Raycast(transform.position, transform.forward, hit))
        {      

             if (hit.collider.gameObject.tag == "z") {      
             var c = true;      
             movement = false;  
             }
             
             if(movement == false){
              GameObject.Find("soldier2").GetComponent(Move_Soldier).enabled = false;
              GameObject.Find("AI follow").GetComponent(Follow).enabled = true;
              
             
             }
   
            }             

}

and this is the new script I made in my enemy

#pragma strict

function Start () {

}

function Update () {

var Script : bulletscript = GetComponent(bulletscript);

if(Script.c == true)
{
GameObject.Find("soldier2").animation.Play("soldierDieBack");

}



}

as you can see I am calling the c boolean in the enemy soldier death script… ):

So if bullet hits target z which it does then c = true, call c in the other script and say if c = true then playd eath animation…

Hi mate, I’m not great on javascript but i think you are getting that error because you are not referring to the object that the bullet script is on.

So if you create a GameObject variable and place the bullet object in it in the inspector…
then you can use that variable when you are calling the script from the bullet.

//GameObject variable to hold a reference to the bullet
var Bullet : GameObject;
 

//Then you can use that to call the script from the bullet in your if statement
if (Bullet.GetComponent(bulletscript).C == true)
{
//Play the animation
}

I’m sure someone will correct me if its wrong, but give it a go and if its working then we can see if the rest of your code is working as intended!

Thank you James, I will try when I get home and will let you know.

But also wanted to ask, I am not calling an 0bject but a boolean??? Is this normal

All I a saying is if bullet hits target then boolean - true and call that boolean in another script.

Yeah let me know!
When you call the boolean from the other script, you need to add the GameObject that you are calling the script from. That’s what i meant with the bit i last posted, is that what you mean?

Oh ok I understand thanks, so next to getcomponent can I sat GameObject.find… and find the game object and get the component from that

Yeah that will work also. I sometimes use findgameobjectwithtag instead of declaring the GameObject at the top.
Doing it that way is useful when you are instantiating and destroying objects because you know it is always going to search the scene for that particular gameobject!

Hey James, I tried it but when I shoot the enemy, the death animation doesn’t play, the soldier just stops and carries on walking in one spot lol

So it’s giving me no errors and the script Boolean is not transferring properly?

This is what I have now

I’ll highlight the important parts

var BulletSpeed = 50; 
var speed : float = 5.0;
var movement = true; 
var c = false; 


function Start () {

}


function Update () {



        transform.Translate(Vector3.forward * Time.deltaTime * BulletSpeed);
        var hit : RaycastHit;
        
        
        if (Physics.Raycast(transform.position, transform.forward, hit))
        {
           if (hit.collider.gameObject.tag == "k") {
             GameObject.Find("soldier").animation.Play("soldierDieBack");
             }
             
             }
            
             
              
            
       [B] if (Physics.Raycast(transform.position, transform.forward, hit))
        {      

             if (hit.collider.gameObject.tag == "z") {
             
             var c = true;            
             movement = false;  
             }
             
             if(movement == false){
              GameObject.Find("soldier2").GetComponent(Move_Soldier).enabled = false;
              GameObject.Find("AI follow").GetComponent(Follow).enabled = true;
              
             
             }
   
            }             
[/B]
}

And the script on my enemy

#pragma strict

var Bullet : GameObject;

function Update () {

var Script : bulletscript = GetComponent(bulletscript);

}



if (Bullet.GetComponent(bulletscript).c == true)

{

GameObject.Find("soldier2").animation.Play("soldierDieBack");

}

So if bullet hits tag z then a = true, it does hit, so if a = true in death script or the 2nd script play death animation, but it doesn;t play in which case it’s not reading it properly?

I dragged the bullet which has the bullet script beside game object in the inspector of the deathscript as well

I;ve been working on this for nearly 2 hours so frustrating, I bet it’s something simple as well, nonetheless thank you so much for your help

At least we have got rid of the error lol. I see on the enemy script you find the soldier2 to play the animation. If the enemy script is already on the soldier that will play that animation then you wouldn’t need to find it. Just play the animation as it will act on the character it is on. Unless I’m getting confused!

You could try having it all in the bullet script. So start c at false, then the first bullet that hits the enemy will meet both of these conditions and play the animation and the next bullets that hit will not.

if (Physics.Raycast(transform.position, transform.forward, hit))

        {      
            if (hit.collider.gameObject.tag == "z")  c == false {

             //Play the dead soldier animation

              //Set C to true so this only happens once

         }

still no solution lol, soldier is standing back up because the bullet always becomes true and I’m guessing the logic resets again? ):

       if (Physics.Raycast(transform.position, transform.forward, hit))
        {      

              if (hit.collider.gameObject.tag == "z"  c == false) {
             GameObject.Find("soldier2").animation.Play("soldierDieBack");
             var c = true;        
             movement = false;  
             }
             
             if(movement == false){
              GameObject.Find("soldier2").GetComponent(Move_Soldier).enabled = false;
              GameObject.Find("AI follow").GetComponent(Follow).enabled = true;
                
              
             
             }
   
            }

Would you say that I even need a script called deathscript on the enemy which is this?

#pragma strict

function Start () {

}

function Update () {

var Script : bulletscript = GetComponent(bulletscript);




}

I am so sorry for muddling you up if I have, just so frustrating ahh

Hi mate, solved it yet? i haven’t been on my pc for a couple days.
You wouldn’t need that script where you getcomponent if you used the method i mentioned last.
How are you using that method combined with the second block of code there? The c bool should stay true after the first time so it only plays the animation once. Not sure why it would reset!
Is the soldier getting back up when you shoot him again or straight away? could it be something to do with (movement == false) if-statement? What is supposed to happen when Follow is set to true?