- Home /
Converting Collider to Bounds variable?
I apparently suck at coding C#, I have absolutely no idea where to even start with fixing this error. I have an idea of what it is, but I don't know what variables I need to put in the middle for it to work. Here is my script:
using UnityEngine;
using System.Collections;
public class AfterInstantiation : MonoBehaviour {
private GameObject control;
private GameManager manager;
private bool overlapping = true;
private GameObject overlapper;
private Bounds bounds1;
private Collider[] hitColliders;
private Bounds bounds2;
void Start(){
overlapper = GameObject.FindGameObjectWithTag("ground");
bounds1 = gameObject.renderer.bounds;
hitColliders = Physics.OverlapSphere(transform.position, 2);
bounds2 = hitColliders;
if (!bounds1.Intersects (bounds2)){
Debug.Log ("Destroying " + gameObject);
Destroy (gameObject);
}
else {
MarkAsInstantiated ();
}
}
void MarkAsInstantiated(){
control = GameObject.FindGameObjectWithTag ("Manager");
manager = control.GetComponent <GameManager> ();
GameManager.dungeonsExisting += 1;
}
}
I am trying to get the object to delete itself if it spawns on top of another object, so I want to draw an OverlapSphere to check if the object is hitting another object, and if it is, I want to delete it. So here is the error I be getting on line 17:
Assets/Scripts/Level Generators/Corridors and Rooms/AfterInstantiation.cs(17,17): error CS0029: Cannot implicitly convert type UnityEngine.Collider[]' to
UnityEngine.Bounds'
I have no idea how to even start trying to fix this error, though it's probably something obvious. Any ideas? Help is much appreciated.
Edit:
If anybody could tell me how to convert hitColliders into all the gameObjects that it hits, I could also go from there.
Try to replace the Collider[] with Bounds [] or at rule 17: bounds2 = (Bounds) hitcolliders. Try this litteraly.
Both of those things end up giving me the same error, sorry. Haha see why I'm so confused?
You have an array of type Collider but which element are you initializing and Bounds isn't an array so therefor you can't convert it.
So I think, then, what I need to do is get the gameObjects of the colliders listed in hitColliders, and then test the bounds of each of those gameObjects. So in that case, how do I get the gameObjects from hitColliders?
Just do like hitcolliders [0] = yourgameObject.collider ot yourgameobject.renderer.collider and for the bounds before yoy ask that yourgameObject.collider.bounds or the secondary way.
Answer by helloiam · Jan 11, 2014 at 07:58 PM
You need to initialize your array in order to interact with bounds2 and then assign that to your bounds2 variable.