- Home /
"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");
}
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);
}
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)
{
...
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.
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.
Yeah it's still the same error. This is the line it highlights - line 47
foreach(Collider hit in colliders)
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.
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);
}
Your answer
Follow this Question
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