Vector3 position doubles in for loop

Hi guys,

I’ve been looking at this piece of code for far too long to the point I’m nearly bald. :smile:

The for loop works fine but the Vector3 positions are double the amount in the editor.

Example:
8, 0, 0 will show as 16, 0, 0
0, 0, 16 will show as 0, 0, 32

Here’s the C# code below.

public List<Transform> CompletedSet = new List<Transform>();
public List<Vector3> CoveSet = new List<Vector3>();

void Start() {

CSCount = CompletedSet.Count;

SetAltar();

}

void SetAltar() {

    if (CSCount > 0) {
           
    // Get Cove List
    for (int gcl = 1; gcl < CSCount; gcl++) {
               
        Vector3 CovePos = CompletedSet[gcl].transform.position;
        float cax = (float)CovePos.x;
        float caz = (float)CovePos.z;
               
        if(CompletedSet[gcl].transform.childCount == 3){
                   
            CoveSet.Add (new Vector3 (cax, 0f, caz));
                                           
        }

    }
           
}

Why are you (float)-ing so much? You don’t need the word float even once.

I’m unable to assess why that problem would come from that code, sorry.

Good point about the floats, don’t know where they came from. :slight_smile:

Code looks fine. Although I’m unsure why you are starting your for loop at index 1 instead of 0, maybe this is the issue, and the value you think is the first value of your List is actually showing the second and so on… And yea you don’t need to put (float)

Maybe show some screenshots of what’s going on in the editor

I’ll do some screen shots. As for the loop starting at 1 it’s to do with the grid (maze) I wanted to leave out the first cell.

A screen shot of the problem. The Cove Set is to the right side.

Ya the float cast is not needed, since that is the type already and you could also just pass covePos.x and y directly into the new vector

Are your transforms children of a scaled object?

Ah, looking at your screenshot, it seems like that is the problem. Grid has a scale of 2, so the position of it’s children will be scaled as well.

1 Like