- Home /
Transform Checking on all Array Objects (JS)
Hello Unity helpers, The scenario goes as follows:
I have Ghost Objects being created with a specific 'y' value on the transform.position I have an array which finds all the Ghost Objects in the scene
GhostFound = GameObject.FindGameObjectsWithTag("GhostTag");
for (var i=0; i < GhostFound.length; i++)
{
SpecificGhost = GhostFound[i];
}
So my question is, how would I get all of the values of the y transform position on all the ghosts in the scene (from that array), and if at least one of the y values matches the number '860', it sets the 'GhostonPlat' boolean to true?
I really do appreciate your help!
Answer by robertbu · Nov 06, 2014 at 09:14 PM
Direct comparison to floats often fails to to floating point imprecision. You can use Mathf.Approximately(), or if there is more chance of imprecision (i.e. moving using Time.deltaTime(), you can look for range. So:
for (var i=0; i < GhostFound.length; i++)
{
var go = GhostFound[i];
if (Mathf.Abs(go.transform.position - 860.0) < someThresholdValue) {
go.GetComponent(Ghost).GhostonPlat = true;
}
}
This assumes that the script with the 'GhostonPlat' variable is named 'Ghost'. Change as appropriate. 'someThresholdValue' will be some small value you set that defines how much the position can vary from 860.0 and still be considered 860.0.
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Reading the other answers, I assumed that the 'GhostonPlat' boolean was on the game object, not in this file. If it is in this file, then line 5 would be just:
GhostPlat = true;
Answer by Uldeim · Nov 06, 2014 at 09:21 PM
First of all, I'd highly recommend NOT doing FindGameObjectsWithTag (or any version of Find anything) pretty much ever, but definitely not during an Update loop. I'd instead recommend adding them to some sort of variable thing when you create them.
Ok, sorry about that. Answer time.
var MagicNumber = 860;
var GhostYs = [];
GhostFound = GameObject.FindGameObjectsWithTag("GhostTag");
for (var i=0; i < GhostFound.length; i++)
{
SpecificGhost = GhostFound[i];
GhostYs.push(SpecificGhost.transform.y);
if (SpecificGhost.transform.y == MagicNumber)
{
GhostOnPlat = true;
break;
}
}
Fairly simple comparison; grab the y value of the transform and compare it (to a variable or const, not a numeric literal cough cough), set the bool and break out of the loop. Break terminates the loop early; if you care about checking every Ghost even after finding one, you can leave it out and the code should work just the same.
You can see I've pushed the Ghost Y values onto an array as well, though if all you needed was to check that one boolean, it's not really necessary.
EDIT: @robertbu has an excellent point. Definitely use some form of float approximation for equals regardless of what you do.
Answer by FairGamesProductions · Nov 06, 2014 at 10:10 PM
Simpler solution:
private var GhostsFound : GameObject[];
private var GhostonPlat = false;
function Start ()
{
GhostsFound = GameObject.FindGameObjectsWithTag("GhostTag");
for (i in GhostsFound)
{
if (i.transform.localPosition.y == 860)
{
GhostonPlat = true;
}
}
}
Sometimes in Unity the locations of gameObjects changes by 0.000000001. So checking directly equals on a transform is not recommended. You should probably check like this:
if (i.transform.localPosition.y > 860.5 && i.transform.localPosition.y < 859.5)
I'd be worried about using localPosition ins$$anonymous$$d of just position...
Well, it depends on what he needs. He can use either localposition or position. The principle stays the same.
Your answer
Follow this Question
Related Questions
How do I create an array for multiple targets? 1 Answer
Instantiate as a child at position 2 Answers
problem moving a prefab object in script (c#) 0 Answers
Position of a GameObject 2 Answers
Locating Position of Object not Working? 2 Answers