Not going inside else condition, please help

This is my script -

this is obstacles.cs

public static bool dead = false;

	void OnTriggerEnter(Collider other) {
		if(other.gameObject.tag == "Player"){
			Debug.Log("died");
			dead=true;
			Debug.Log("dead= "+dead);   // i am getting this message dead = True in console
			Destroy(gameObject);
			
			foreach(GameObject go in   GameObject.FindGameObjectsWithTag("obstacle")) { Destroy(go); }
			foreach(GameObject go in   GameObject.FindGameObjectsWithTag("bottle")) { Destroy(go); }
			foreach(GameObject go in   GameObject.FindGameObjectsWithTag("powerup1")) { Destroy(go); }
			foreach(GameObject go in   GameObject.FindGameObjectsWithTag("powerup2")) { Destroy(go); }
			foreach(GameObject go in   GameObject.FindGameObjectsWithTag("drunk")) { Destroy(go); }
			foreach(GameObject go in   GameObject.FindGameObjectsWithTag("nondrunk")) { Destroy(go); }
	
    }}

this is the script where else is not working, allMovementScript.cs

 public float runSpeed = 8.0f;
              
        // Update is called once per frame
        void Update ()
        {		
              if(obstacles.dead==false){
		    transform.Translate(Vector3.forward * Time.deltaTime*runSpeed);
			Debug.Log("Not dead");
		}
			 else{
			transform.Translate(Vector3.forward *0.0f);
			Debug.Log("x....dead");
				
		}

why i dont see this message in console

Debug.Log(“x…dead”);

Please help me

If you don’t see a “Not dead” or a “x…dead” message in the console the allMovementScript.cs script isn’t in the scene or its not enabled.
Also make sure you don’t have the collapse messages on the console, this might causes you to overlook any new messages because they are getting collapsed.

Lastly, I wouldn’t make the dead variable static as that means its not per instance.

i see “Not dead”, but i dont see “x…dead”,

This is my class >> public class obstacles : MonoBehaviour {

and if i remove static, then i get this error-

Assets/scripts/allMovementScript.cs(63,28): error CS0120: An object reference is required to access non-static member `obstacles.dead’

what do i do ?

if you don’t want to use a static variable (which may or may not be right for your scene, you just have to remember this variable isn’t independent so can only be used for 1 thing, which sometimes is perfect) then you need to use getcomponent to find the script and then address the variable in the instance of the script you create in the script
EG.

var collectScript : CollectTiles;
    collectScript = GetComponent(CollectTiles);

then in my example you would go collectScript.dead

You could remove the else and just have the code there but add a “return;” in the if statement. Though I’m to tired to think if you can return in update. Or you could just add the opposite if statement.

The reason for this script not working is:
public
in “public static bool dead = false;”

Once you declared it as public and its visible in the inspector you can’t change it anymore - not even within the code. Therefore your “dead” will always be false. Always and ever. It should be mentioned in red underlined characters in the Unity docs, that this happens when you use a public variable, but it isn’t. Mostly you only find it out only through trial and error. It’s one of the most evil traps.

Solution: either make it private or non-serialized

How is that true? You can change it via code very easily. Also i thought static does not show up in the inspector…

To the question asked, simply put this in Awake on your obstacles.cs

dead = false;

The problem here is almost certainly misuse of “static”. Judging by the apparent purpose of the code that variable should not be static.

Unless you fully understand what “static” does you should not use it. Pretend it’s not there. It’s almost never the solution to your problem, and if it seems to be then chances are you’re better off re-approaching the problem anew at the design level rather then the code level. Static (ie: global) access to anything is a good way to get yourself in a mess unless you’re incredibly sure of what you’re doing and are happy to throw away a bunch of tools that will otherwise make your life easier.

From looking at what you’re doing you should be using an instance variable rather than a static one (so just “public bool dead”) and then get the rest of your logic right and you’ll be fine.