Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by VonDougy · Dec 11, 2014 at 05:13 PM · boolgrenade

"Cannot implicitly convert type 'UnityEngine.Collider[]' to 'bool'"

I'm trying to implement a frag grenade into my FPS. I want it so that the barrels will be affect by force then explode and add 1 to the player's score. This is the part of code with the error.

     IEnumerator DelayedExplosion(float delay)
     {
                 {
                     yield return new WaitForSeconds (delay);
                         Instantiate (fragExplosion, grenade.transform.position,
                          Quaternion.identity);
                         grenade.SetActive (false);
                         audio.clip = grenadeExplosion;
                         audio.Play ();
                             Vector3 explosionPos = grenade.transform.position;
                             Collider colliders = Physics.OverlapSphere (explosionPos, radius);
                             foreach(Collider hit in colliders)
                                 {
                                      if(Physics.OverlapSphere (explosionPos, radius)){ 
                                         if (hit.collider.gameObject.tag == "Target"){ 
                                             Debug.Log ("Hit" + hit.collider.gameObject.name);
                                             TargetScript targetScript =
                                                 hit.collider.gameObject.GetComponent<TargetScript> ();
                                             if (targetScript != null) {
                                                 targetScript.HitApplyForce(grenade.transform.position, 100f);
                         }
 
                         GameObject playerScript = GameObject.Find ("Player");
                         {
                             
                             PlayerScript Playerscript
                                 = playerScript.GetComponent<PlayerScript>();
                                         
                                     Playerscript.score++;
 
                                 }
                     }
                         yield return new WaitForSeconds (audio.clip.length);
                         yield return new WaitForSeconds (0.5f);
                 
                 }
             }
 
         }
         StopCoroutine ("DelayedExplosion");
     }

Comment
Add comment · Show 1
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 VonDougy · Dec 11, 2014 at 08:22 PM 0
Share

Fixed the error however it behaves completely wrong. No force is applied to the barrels until after a couple of seconds and it only happens to one barrel at a time.

 IEnumerator DelayedExplosion(float delay)
     {
                 {
                     //Blowing up the grenade and affect object around it
                     yield return new WaitForSeconds (delay);
                         Instantiate (fragExplosion, grenade.transform.position,
                          Quaternion.identity);
                         grenade.SetActive (false);
                         audio.clip = grenadeExplosion;
                         audio.Play ();
                             Vector3 explosionPos = grenade.transform.position;
                             Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);
                             if(colliders != null)
                             {
                             foreach(Collider hit in colliders)
                                 {
                                             Physics.OverlapSphere (explosionPos, radius);
                                         if (hit && hit.rigidbody)
                                             hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 5F);
                                         if (hit.collider.gameObject.tag == "Target"){ 
                                             Debug.Log ("Hit" + hit.collider.gameObject.name);
                                             TargetScript targetScript =
                                                 hit.collider.gameObject.GetComponent<TargetScript> ();
                                             if (targetScript != null) {
                                                 targetScript.HitApplyForce(grenade.transform.position, 1f);
                         }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Dec 11, 2014 at 05:22 PM

It's because Pysics.OvelapSphere returns an array and you are trying to use it as a bool

  if(Physics.OverlapSphere (explosionPos, radius))

Also, aren't you doing that same OverlapSphere just 3 rows before that ? Then you do the same check for all colliders inside the collider list you get as a result. The code doesn't make any sense to me.

 Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);
 foreach(Collider hit in colliders)
 {
     if(Physics.OverlapSphere (explosionPos, radius)) {...

It should probably be something like

 Collider colliders = Physics.OverlapSphere (explosionPos, radius);
 if (colliders != null)
 {
     foreach (Collider hit in colliders)
     {
         ...

Comment
Add comment · Show 6 · 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 VonDougy · Dec 11, 2014 at 05:31 PM 0
Share

You mean like this?

    Collider colliders = Physics.OverlapSphere (explosionPos, radius);
                             if(colliders != null)
                             {
                             foreach(Collider hit in colliders)
                                 {
 
                                         if (hit.collider.gameObject.tag == "Target"){ 
                                             Debug.Log ("Hit" + hit.collider.gameObject.name);
                                             TargetScript targetScript =
                                                 hit.collider.gameObject.GetComponent<TargetScript> ();
                                             if (targetScript != null) {
                                                 targetScript.HitApplyForce(grenade.transform.position, 100f);



That still gives me the same error.

avatar image NoseKills · Dec 11, 2014 at 05:50 PM 0
Share

Actually Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);

I edited the answer. Is it giving the exact same error or was it complaining about the Collider vs. Collider[] thing ?

If you double click the error in Unity console, which line does it take you to ? that way it should be pretty easy to see where the problems are.

avatar image VonDougy · Dec 11, 2014 at 05:55 PM 0
Share

Yeah it's still the same error. This is the line it highlights - line 47

     foreach(Collider hit in colliders)
avatar image NoseKills · Dec 11, 2014 at 08:11 PM 0
Share

That doesn't make any sense since there's nothing in a foreach that needs a bool as parameter. On the other hand the if(Physics.OverlapSphere (explosionPos, radius)) I mentioned should give you exactly the error you are describing.

Did you clear your console (clear button at the top of the console window) after the code changes ? Are there any other errors that might be preventing compiling so you still get the old error. $$anonymous$$ake sure to save all classes and maybe even restart Unity.

avatar image VonDougy · Dec 11, 2014 at 08:17 PM 0
Share

I managed to get rid of the error by doing this, however it doesn't behave right at all. The grenade explodes and then the barrels react one at a time with 2 second intervals.

 IEnumerator DelayedExplosion(float delay)
     {
                 {
                     //Blowing up the grenade and affect object around it
                     yield return new WaitForSeconds (delay);
                         Instantiate (fragExplosion, grenade.transform.position,
                          Quaternion.identity);
                         grenade.SetActive (false);
                         audio.clip = grenadeExplosion;
                         audio.Play ();
                             Vector3 explosionPos = grenade.transform.position;
                             Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);
                             if(colliders != null)
                             {
                             foreach(Collider hit in colliders)
                                 {
                                             Physics.OverlapSphere (explosionPos, radius);
                                         if (hit && hit.rigidbody)
                                             hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 5F);
                                         if (hit.collider.gameObject.tag == "Target"){ 
                                             Debug.Log ("Hit" + hit.collider.gameObject.name);
                                             TargetScript targetScript =
                                                 hit.collider.gameObject.GetComponent<TargetScript> ();
                                             if (targetScript != null) {
                                                 targetScript.HitApplyForce(grenade.transform.position, 1f);
                         }


Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OverlapSphere not causing damage? - Solved 2 Answers

is there a way to change the public inspector details at runtime 1 Answer

How do I disable one button out of multiple buttons without spamming declaration? 1 Answer

C# Boolean Constantly Changing 1 Answer

Writing a bool to a separate script without scripts name? 3 Answers


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