- Home /
foreach-loop. Is there a gameobject with position existing?
Hey!
I'm working around with arrays and I would like to know how to look for a gameobject by its Position.
Example Scriptpart:
public GameObject[] weels;
void Update()
{
foreach(GameObject weel in weels)
{
//Check if there a weel existing with Position Vector(x,y,z)
}
}
Please help!
thanks in advance!
maybe:
if(weel.transform.position == new Vector3(x, y, z))
// do stuff
I need something like that
if(weel.position[x,y,z] == null)
{
//DO SO$$anonymous$$ETHING
}
else
{
//DO NOTHING
}
Answer by glennhk · May 02, 2015 at 03:37 PM
Check the distance between every wheel in the array and the target position using Vector3.Distance, or by computing the magnitude (sqrMagnitude is better for efficiency), and check if the distance is within a fixed threshold.
sorry, but that doesen't help me. pls look to upper comment. tks
Answer by Bunny83 · May 02, 2015 at 03:42 PM
You can't directly access a gameobject by it's position. You also shouldn't compare the position of gameobjects with a given coordinate as due to floating point accuracy issues this won't work. What you usually do is specifying a limit / range to look for a gameobject.
public GameObject[] weels;
public Vector3 somePosition;
void Update()
{
foreach(GameObject weel in weels)
{
if ((weel.transform.position - somePosition).magnitude < 1f)
Debug.Log("The object " + weel.name + " is closer than 1 unit to " + somePosition);
}
}
You can also use the Distance method which does the same thing:
if (Vector3.Distance(weel.transform.position, somePosition) < 1f)
Yeah, I know what u want to do, but in my game, I need to check first if there is a gameobject existing in the position....
That approach is clearly error-prone. There can be 0, 1, 2, etc. GameObjects in a single point. If you need to query the nearest GameObjects in a position, put a collider on each wheel, configure a layer for the wheels in order to disable unwanted collisions and use an OverlapSphere on that particular layer's mask.
Answer by Addyarb · May 02, 2015 at 05:14 PM
As you can see from the other responses, using Vector3 positions sets you up to run into all sorts of problems, not to mention it's inefficient.
If you don't mind, I'll suggest an (imo) easier/better way, and you can comment if this wouldn't work in your particular situation. I think the best way to achieve this is to completely scrap the idea of locating an object via Vector3 and use Tags instead.
So my solution would look something like this:
Create a new tag called "Wheel."
Tag each of your wheels as "Wheel."
Use this script to get them into an array and access them if they're extant.
Be sure and attach this script to the root gameObject (your car?) or alter the script a bit in order to check the wheels.
GameObject[] wheels; //The wheels on my car void Start() { //find all the wheels on my car by searching their tag. wheels = GameObject.FindGameObjectsWithTag("Wheel"); } void Update() { foreach(GameObject wheel in wheels){ //If the wheel exists, and it is attached to this car.. if(wheel.activeInHierarchy && wheel.transform.root == transform){ //this code will only happen if wheel is enabled } } }
Your answer
Follow this Question
Related Questions
Logic question, unique path finding system 2 Answers
How can I do a foreach loop for an array of booleans? 1 Answer
Guarantee loop order of child objects. 1 Answer
foreach Gameobject in array problems 1 Answer
Foreach with GameObject.Find() 1 Answer