Here I go again…
This time I was making a script that checks if there’s an object in the same coords as the one with the script attached and it ended like this:
But it worked just half well because, even if it gave the true that I wanted, I didn’t found a way to make it return to false when no object was at its coords, so I tried to put a “else { ocupado = false }”, but it would always return false and I got without ideas after that. I need it to work with a list of objects that move around and I’m fairly newbie on unity.
First, position is a bunch of floats, so checking for equality is going to be fraught with peril.
Use something like Mathf.Approximately instead
Second, you are checking the same position every iteration of the for loop. It looks like CharacterList.Instance.Pers is a list, so you’ll want to use the indexing operator to get the next position every time. Once you’ve done that, you should put your ‘else { ocupado = false; }’ back in.
Third, the second if check, "if(i==p), is never going to be true, since your for loop is only looping while i is less than p. I’m not even sure what the purpose of this section of code is. I think it doesn’t belong.
And finally, you’re setting ocupado every iteration of the loop, but not doing anything with it. So by the end of the loop, ocupado will be whatever the last iteration set it to. Consider revising your logic.
Edit: I just wrote this up in notepad without checking the unity reference files or trying to compile. I make no guarantees as to its actual usefulness.
void Update ()
{
position = this.transform.position;
p = CharacterList.Instance.Pers.Length;
for (i = 0; i < p; i++)
{
charposition = CharacterList.Instance.Pers[i].transform.position;
if (Mathf.Approximately(charposition.x, position.x) && Mathf.Approximately(charposition.z, position.z))
{
ocupado = true;
}
else
{
ocupado = false
}
// you could simplify this as:
// ocupado = Mathf.Approximately(charposition.x, position.x) && Mathf.Approximately(charposition.z, position.z);
}
}
If all you need is to figure out if you have at least one match, just put a break; after the ocupado = true to break out of the for loop.
This way as soon as you find a match, you break out. The reason you’re getting false is even if you hit true, something later in the for loop might set it to false. @Habitablaba just posted some code before me, but without breaking out of the loop, you’ll run into the same issue that if the last object in your Pers array doesn’t match, you’ll always end in false. (Note he does make a good point on comparing position)
It just depends on what you want. Are you just trying to see if at least one position matches, all positions, etc?
Honestly, do you need to check every frame if something is next to the position?
1- If I use “Mathf.Approximately” hen it’ll activate when the distance is lower than 0.5 ?
2- I didn’t got this part…
3- It’s just some remaints from an old try of mine, don’t worry about it.
4- If I understood it right, are you telling me that it’s the wrong way of doing it ? Or are you asking me why I don’t use the “ocupado” for nothing ? If it’s the first one, what would be a better way ?
1- What do you mean by break ? I’m really really newbie on it…
2- I want to check if at least one match (Actually, I would have done something wrong if more than one matches).
3- Since the Blocks(I have a bunch of cubes moving through the map) will be moving all the time, I thought that it would be needed if I wanted to know when one of them is near to the Hexagon, or it isn’t ?
By the way, I really fell into the same problem from before with @Habitablaba 's code, so I really would be glad if you explained me that break thing and really thanks to both of you for the help till now.
OBS: My english isn’t something very impressive, so I can have wrote something wrong here or there.
If you’re making a grid-based game you could also consider making each cell of the grid an object that can be occupied by a character. Then you could simply reference the cell directly through it’s index and check if it’s occupied and who is occupying it.
If not you could simply use Vector3.Distance to see if any of the characters close the specified position.
I was thinking the same as @Vipsu as well as some other thoughts but it all depends on how you are implementing this and what your goals are. It is hard to give advice with such little information. If it is a grid layout type deal I would do as Vipsu stated. If you are moving around a 3D room and this is to see for instance if a chair is being sat in already by another character I would use a trigger method that kept the occupied bool state for that object.
As I mentioned, if you put break; within a loop, when it gets called it breaks out of the loop. So by putting it after where you set ocupado = true; then it will break out of the for loop. You do this so the loop stops and basically says, Hey, I found something, there is no need to check the rest of the objects.
For the cubes moving, it may be better to use colliders with a trigger element, but as some have mentioned, without knowing more of the design, it’s hard to suggest a better method.