- Home /
The question is answered, right answer was accepted
Check bool from multiple instances of the same script
Hello, I am trying to check if a bool on a script is false if it leaves the collider. The "dropzones" objects all have the tag "dropzone" and the script "DropZone1" is in the scene multiple times, attached once to each of the dropzone objects. In their Start Function, I have written satisfied as false. My boundary script looks like this:
public class DestroyByBoundary : MonoBehaviour
{
GameObject[] dropzones;
DropZone1 dropzonescript;
GameController gameController;
private void Start()
{
gameController = FindObjectOfType<GameController>();
dropzones = GameObject.FindGameObjectsWithTag("dropzone");
foreach (GameObject zones in dropzones)
{
DropZone1 dropzonescript = zones.GetComponent<DropZone1>();
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "dropzone")
{
verifyifSatisfied(dropzonescript.satisfied);
}
}
void verifyifSatisfied(bool satisfied)
{
if (dropzonescript.satisfied == false)
{
gameController.NoAnswer();
gameController.DecideIfDisplay();
}
}
}
I would like to make it so if one of those "dropzones" leaves the collider, it calls that "verifyifsatisfied" function. However, whenever I test, the code has a nullreferenceexception. Is there a way to fix this?
Answer by Frank-1999-98K · Jan 06, 2019 at 02:58 AM
From what I can understand, your`DestroyByBoundary` script should look like this:
public class DestroyByBoundary : MonoBehaviour {
GameController gameController;
private void Start () {
GameController = FindObjectOfType<GameController> ();
}
void OnTriggerExit (Collider other) {
if (other.gameObject.CompareTag ("dropzone")) { //is better to use ".CompareTag ("YourTag")" then ".tag == "YourTag"
if (other.getComponent<DropZone1> ()) {
verifyifSatisfied (other.getComponent<DropZone1> ().satisfied);
}
}
}
void verifyifSatisfied (bool satisfied) {
if (!satisfied) { //"!satisfied" is the same as "satisfied == false"
gameController.NoAnswer ();
gameController.DecideIfDisplay ();
}
}
}
And to make it even more polished, you could even summarize it like this:
public class DestroyByBoundary : MonoBehaviour {
GameController gameController;
private void Start () {
GameController = FindObjectOfType<GameController> ();
}
void OnTriggerExit (Collider other) {
if (other.gameObject.CompareTag ("dropzone")) {
if (other.getComponent<DropZone1> () && other.getComponent<DropZone1> ().satisfied) { {
gameController.NoAnswer ();
gameController.DecideIfDisplay ();
}
}
}
}
Let me know if this helps.
Thanks for the pointers! I'll use CompareTag from now on. Also, this script works perfectly! Thank you! For some reason GetComponent pasted in a lowercase g so that was all I had to fix. Again, thank you so much!
@Frank-1999-98$$anonymous$$ I'm intrigued by this. You optimize the way you compare tags, but prefer to call GetComponent multiple times in the same function? ;)
Answer by Ady_M · Jan 06, 2019 at 02:46 AM
Try something along the lines of this.
I still don't know what the foreach is for, though.
public class DestroyByBoundary : MonoBehaviour
{
GameObject[] dropzones;
//DropZone1 dropzonescript; // Ady: Delete this
GameController gameController;
private void Start()
{
gameController = FindObjectOfType<GameController>();
dropzones = GameObject.FindGameObjectsWithTag("dropzone");
foreach (GameObject zones in dropzones)
{
// Ady: I have no idea what you were trying to do here
//DropZone1 dropzonescript = zones.GetComponent<DropZone1>(); // Ady disabled code
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "dropzone")
{
// Ady: Gets the DropZone1 script that's attached to the object that exited the trigger
var dropzonescript = other.GetComponent<DropZone1>(); // Ady
verifyifSatisfied(dropzonescript); // Ady
}
}
void verifyifSatisfied(DropZone1 dropzonescript) // Ady: Replaced parameter with DropZone1 type
{
if (dropzonescript.satisfied == false)
{
gameController.NoAnswer();
gameController.DecideIfDisplay();
}
}
}
Yeah, your answer It's along the same lines of $$anonymous$$e, but I think I understood what he was trying to do and I cleaned it up a bit more..
It should work in both of our ways though
Hello, thank you for taking the time to answer!
What I was trying to do by putting the foreach loop in Update was that there would be a new "dropzone" gameObject instantiated every 5 seconds, so I was trying to have it find the script attached all the dropzones constanty. Also, unfortunately I'm also getting another NullReferenceException, this time on the line that says
if (dropzonescript.satisfied == false)
For clarity, my dropzonescript looks like this
public class DropZone1 : $$anonymous$$onoBehaviour {
//Referencing the GameController so we can use its functions. I'm giving it the name gameController, so whenever we reference it in here we write gameController.
private GameController gameController;
private Collider dropZoneCollider;
public bool satisfied;
void Start()
{
dropZoneCollider = gameObject.GetComponent<Collider>();
gameController = FindObjectOfType<GameController>();
satisfied = false;
}
//This function happens whenever another thing enters the trigger collider.
void OnTriggerEnter(Collider other)
{
//When something enters this object's collider, do the OnTriggerEnter from the gameController ins$$anonymous$$d of our own.
gameController.OnTriggerEnter(other);
if (other.gameObject.tag.StartsWith("box"))
{
dropZoneCollider.enabled = false;
satisfied = true;
}
}
}
And it runs error free so I don't understand how it still has a NullReferenceException. Do you know what could be causing that?
Sorry about the NullReference in my code. I accidentally wrote GetComponent ins$$anonymous$$d of other.GetComponent. I fixed it.
Hi! After this was written, a different answer was posted that was written in a completely different way than I had written it and it works fine! However, I'm not gonna close the question yet because I would still like to know what is causing that NullReferenceException. If you can find that, please let me know!
Did you update your code so it fixes the other.GetComponent part?