- Home /
Bounds.Contains not working properly
I have a RoomManager script whose job is to track and manipulate all objects inside the room, which is defined by a box trigger collider that the script requires. Every piece of furniture has a collider, and I'm trying to iterate through my furniture and compile a list of every piece inside the collider, as follows:
furnitureHolding = GameObject.FindGameObjectsWithTag ("Furniture");
for (int i = 0; i < deskHolding.Length; i++) {
if (Bounds.Contains(furnitureHolding[i]))
furnitureList.Add(furnitureHolding[i]);
}
However, the line with Bounds.Contains prompts an error, "An object reference is required to access non-static member UnityEngine.Bounds.Contains(UnityEngine.Vector3)". Is my syntax somehow off, or am I just using bounds.contains in a way it wasn't intended for?
Answer by dsada · Aug 03, 2014 at 09:20 AM
Contains is not a static function of the Bounds class. You are trying to use like it was. You need a Bounds instance and then you can call that. You said you have a collider so i will assume you are using the collider's Bounds. In this case you should do this:
Bounds myBounds = yourCollider.bounds; //yourCollider here is the collider your script requires.
furnitureHolding = GameObject.FindGameObjectsWithTag ("Furniture");
for (int i = 0; i < deskHolding.Length; i++) {
if (myBounds.Contains(furnitureHolding[i]))
furnitureList.Add(furnitureHolding[i]);
}
So this gets rid of the error, and fixing the typo mentioned in the post below everything works 100% perfectly- thank you so much for the clarification! I had mistakenly assumed that Bound was a function, not a type.
Answer by Taylord303 · Aug 03, 2014 at 09:51 AM
I haven't used bounds much, but I believe it is supposed to be used Collider.bounds.
Saying nothing is better than saying something wrong.
I'm sorry, I'm just trying to help. There's no need to be rude. If you have some great input, please, present it. Otherwise, there is no need for you to comment.
Answer by gjf · Aug 03, 2014 at 09:23 AM
what type is furnitureHolding
defined as?
Bounds
is a type - you need a variable containing the bounding box.
Bounds boundingBox = new Bounds(Vector3.zero, new Vector3(1, 2, 1));
then
if (boundingBox.Contains(furnitureHolding[i]))
you are iterating thru furnitureHolding
using deskHolding.Length
as a count - that's an accident waiting to happen ;)
also, which line is the error reported on?