- Home /
Checking if object intersects?
Okay I thought I had this all figured out late last night, but coming back today I think I was dead wrong. And before you laugh at my script, I have only been doing my own scripting in C# for a little over a week. Anyway, I can't figure out how to check whether or not an object is overlapping another object, and based on that either delete the object or mark it as instantiated in another script. So here's the script:
using UnityEngine;
using System.Collections;
public class AfterInstantiation : MonoBehaviour {
private GameObject control;
private GameManager manager;
private bool overlapping = true;
void Start(){
if (!overlapping){
Destroy (gameObject);
}
else {
MarkAsInstantiated ();
}
}
void MarkAsInstantiated(){
control = GameObject.FindGameObjectWithTag ("Manager");
manager = control.GetComponent <GameManager> ();
GameManager.dungeonsExisting += 1;
}
bool Intersects(Bounds bounds){
overlapping = bounds;
}
}
This script only gave me one error, so I decided to show this one. This is the error I get, if it helps:
Assets/Scripts/Level Generators/Corridors and Rooms/AfterInstantiation.cs(28,17): error CS0428: Cannot convert method group Intersects' to non-delegate type
bool'. Consider using parentheses to invoke the method
I think the problem is I just don't know how to call variables from other functions or something simple, but could anybody help me figure out what's wrong with this script? Thank you in advance.
Answer by robertbu · Jan 11, 2014 at 04:15 PM
The problem is line 28. You are assigning a variable of type 'Bounds' to a variable of type 'bool'. Can't do that. I'm not sure what two bounds you are going to compare to get the intersection. The bounds of the collider maybe? Anyway the syntax to compare two bounds:
overlapping = bounds1.Intersects(bounds2);
It is strange to have a function both set an instance variable to a value and return the value, but maybe you want:
bool Intersects(Bounds bounds){
overlapping = collider.bounds.Intersects(bounds);
return overlapping;
}
Okay I think this is getting me somewhere. So now there is this problem. I think I could use that first line of code you wrote as long as I could define the object for bounds2, but since the level is randomly generated, there will never be a set bounds2 object. Is there a way I could say something like:
if (bounds1.Intersects(**any object**))
or maybe
if (bounds1.Intersects(**any object with a certain tag**))
You would have to first get a list of the objects (GameObject.FindGameObjectsWithTag()), and then test each one. As an alternate, take a look at Physics.OverlapSphere().
That Physics.OverlapSphere is awesome! I think I can get this to work, thank you very much for you help.
Your answer
Follow this Question
Related Questions
Object spawn issues 1 Answer
overlapping object doesn't disable 0 Answers
overlapping object doesn't disable 1 Answer
How to make objects spawn at the same time without colliding ? 1 Answer
Spawn GameObjects without overlap 1 Answer