Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 06, 2019 at 04:37 AM by dcw2001 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by dcw2001 · Jan 06, 2019 at 02:20 AM · c#scripting problemcolliders

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dcw2001 · Jan 06, 2019 at 03:45 AM 1
Share

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!

avatar image Ady_M · Jan 06, 2019 at 04:25 AM 0
Share

@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? ;)

avatar image
0

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();
         }
     }
 }
Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Frank-1999-98K · Jan 06, 2019 at 03:01 AM 1
Share

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

avatar image dcw2001 Frank-1999-98K · Jan 06, 2019 at 03:41 AM 1
Share

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?

avatar image Ady_M dcw2001 · Jan 06, 2019 at 04:17 AM 1
Share

Sorry about the NullReference in my code. I accidentally wrote GetComponent ins$$anonymous$$d of other.GetComponent. I fixed it.

Show more comments
Show more comments
avatar image dcw2001 · Jan 06, 2019 at 03:51 AM 1
Share

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!

avatar image Ady_M dcw2001 · Jan 06, 2019 at 04:36 AM 0
Share

Did you update your code so it fixes the other.GetComponent part?

Follow this Question

Answers Answers and Comments

592 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Check bool from multiple instances of the same script 0 Answers

Why when i move the player object through the door the ontriggerenter/exit event are not fire ? 2 Answers

Add audio. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges