How to compare a position of an object within an array
Hello, so i was trying to compare a position of an object within an array with an int variable that i have
I have this variable called stagePos, and i wanna use it to find an object that has the same value for its position in an array.
If the stagePos is 1, then i want to find an object that has the position of [1] in an array and do something with it as well as do something with the other object in that array because they don't have the same value as stagePos
if(stage[i] != stage[stagePos]){
Vector3 pos = stage[i].transform.position;
pos += new Vector3(0f, -4f * Time.deltaTime , 0f);
stage[i].transform.position = pos;
stageScript.Invinsible(true);
}
else if(stage[i] == stage[stagePos]){
Vector3 pos = stage[i].transform.position;
pos += new Vector3(0f, 4f * Time.deltaTime, 0f);
stage[i].transform.position = pos;
stageScript.Invinsible(false);
}
So that is the code, but i still can't figure out on what do i need to do to compare it
Answer by tormentoarmagedoom · Mar 04, 2020 at 03:51 PM
Hello.
I'm not sure to understand you.
StagePos i an integrer? where it comes from? You want to compare if stage[i] position is the same as stage[stagePos] position?
Then just
if (stage[i].transform.position == stage[stagePos].transformposition)
or you need to find something like
¿which element from all stage[] objects have the same trasfrom as stage[stagePos]?
then do something like:
void findStageWithSamePos()
{
foreach (GameObject OneStage in stage[])
{
if (OneStage.transform.position == stage[stagePos])
{
// DO something here, because OneStage position will be same as stage[stagePos]
}
}
No it's not like that
Basically i want to check, when i use stagePos its default value is 0, and i get an input from user to increment it to 1, i wanna check whether there's an object in the stage array that has the position of 1, if there is then i wanna do something to it but i also wanna do something to the other object that has a different position after it did something, in this case is just moving the object around i want the code to just sit there and wait until there's a change with the stagePos value, and then do it all over again
$$anonymous$$an.. your explanation si not very good...
"there's an object in the stage array that has the position of 1" of 1 what ? position is a vector, not a number. how can have position of 1? If there is an object with the same position of first element from the array?
You have multiple problems., go step buy step. TRy to say in one sentece what 2 elements need to compare, and talk correctly, using the correct words. If not is almost impossible to help you. And this its like a conversation, and shouldnt. Pelase remake the post giving the correct info. (we are close to solve your problem :D)
Okay it's like this, you know that when you put something in an array, that something has an index position within an array as [0]. let's say that i have a public GameObject[] food; if i put noodles inside the food variable, i can access the noodles with just food[0] right ?
So that's what i'm trying to do, i want to access an index position of an object within the array, not the transform position of the object itself because i want to basically do something with the object that i choose the index position with a stagePos variable
So that when the stagePos value is [0], it will access the food[0], and anything that isn't food[0] like food[1], food[2] won't do anything
Your answer
Follow this Question
Related Questions
Dynamic buttons with an array from firebase URL links 0 Answers
Moving spawned enemies randomly towards several pre-defined positions 1 Answer
Array problem, I'm novice 1 Answer
Storing a Gameobject from array into a Gameobject variable giving NullReferenceException 3 Answers
Problems with arrays (or how to rewind objects' states) 0 Answers