- Home /
public static bool not working at all.
Why isn't Debug.Log("The Box is Destroyed");
not getting called at all?
Here's the code:
Player Script
public class Player : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
//If it hits the Box
if (col.gameObject.CompareTag ("Box"))
{
//This gets called
Debug.Log ("Destroy the box!");
//This doesn't, however
Box.destroyed = true;
}
}
}
Box Script
public class Box : MonoBehaviour {
public static bool destroyed;
void Start()
{
destroyed = false;
}
void Update()
{
if (destroyed)
{
//This doesn't get called even when the Player hits the Box, I have no idea why
Debug.Log("The Box is Destroyed");
}
}
}
Why does Debug.Log ("Destroy the box!");
get called... but Box.destroyed
doesn't?
Or maybe it does get called, but the public static bool on the Box Script is not working.
Am I missing something here?
You could debug this by making destroyed
a property ins$$anonymous$$d of a variable.
Replace the line
public static bool destroyed;
with
public static bool _destroyed;
public static bool destroyed
{
get
{
Debug.Log("destroyed was " + _destroyed);
return _destroyed;
}
set
{
Debug.Log("destroyed was set to " + value);
_destroyed = value;
}
}
I couldn't see anything wrong with your scripts, so I did a quick setup and ran the scripts. No problems, so there is something specific in your setup that is causing the issues. Are you sure the 'Box' script is on the other object?
Your answer
Follow this Question
Related Questions
Access a variable from another script in update function 1 Answer
Help With C# Static Boolean! 2 Answers
Probably Dumb Question: Update(), Start(), etc. Don't Work Inside Classes (JavaScript)...? 2 Answers
A node in a childnode? 1 Answer
an object reference is required for the non static field error 1 Answer