Make sphere collider the center of gameobjects/Sum of vectors

Hi, I have problems setting the center of a sphere collider at the center of spawned gameobjects, it moves but not to the center of the gameobjects.

edit: I’ve found that the sum of Vectors doesn’t give the correct number, thats the problem, but i don’t know why it happens

This is the code:

void SphereRadious()
    {
        //4/3 * pi * r^3 = V ; Vt =  V * (protons + neutrons) ; r^3 = Vt/(4/3 * pi) ; r = sqrt(Vt/(4/3 * pi))
        float atomVol = 4 / 3 * Mathf.PI * 1;
        float totalVol = atomVol * (protonsAmount + neutronsAmount);
        float r = Mathf.Pow(totalVol / ((4 / 3) * Mathf.PI), 1f/3f);
        sphereCollider.radius = r;
        radiousOfSpawn = r;

        //PROBLEM:

        float x = 0;
        float y = 0;
        float z = 0;


        for (int p = 0; p < protonsAmount; p++)
        {
            for(int n = 0; n < neutronsAmount; n++)
            {
                x = 0; y = 0; z = 0;
                x += (protons[p].transform.localPosition.x + neutrons[n].transform.localPosition.x);
                y += (protons[p].transform.localPosition.y + neutrons[n].transform.localPosition.y);
                z += (protons[p].transform.localPosition.z + neutrons[n].transform.localPosition.z);
                sphereCollider.center = new Vector3(x, y, z);
            }
        }
    }

139515-capture.png

edit2: i’ve also tried this but the same result:

        float x = 0;
        float y = 0;
        float z = 0;
        

        for (int p = 0; p < protonsAmount; p++)
        {
            float xp = protons[p].transform.localPosition.x;
            float yp = protons[p].transform.localPosition.y;
            float zp = protons[p].transform.localPosition.z;
            x += xp;
            y += yp;
            z += zp;
        }

        for (int n = 0; n < neutronsAmount; n++)
        {
            float xn = neutrons[n].transform.localPosition.x;
            float yn =  neutrons[n].transform.localPosition.y;
            float  zn = neutrons[n].transform.localPosition.z;
            x += xn;
            y += yn;
            z += zn;
        }
        sphereCollider.center = new Vector3(x, y, z);

Hopefully this is what you mean?

    public GameObject[] GOs;
    private float totalX = 0f;
    private float totalY = 0f;
    private float totalZ = 0f;

    public Vector3 center; // This is the center of all the objects 

    void Start()
    {
        foreach (GameObject GO in GOs)
        {
            totalX += GO.transform.position.x;
            totalY += GO.transform.position.y;
            totalZ += GO.transform.position.z;
        }
        center.x = totalX / GOs.Length - 1;
        center.y = totalY / GOs.Length - 1;
        center.z = totalZ / GOs.Length - 1;
    }

    void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(center, 0.5f);
    }

So the sphere collider is on the parent of all the neutrons and protons, correct?? In the first script, you are setting x y and z to 0 on every iteration. Try this:

 void SphereRadious()
     {
         //4/3 * pi * r^3 = V ; Vt =  V * (protons + neutrons) ; r^3 = Vt/(4/3 * pi) ; r = sqrt(Vt/(4/3 * pi))
         float atomVol = 4 / 3 * Mathf.PI * 1;
         float totalVol = atomVol * (protonsAmount + neutronsAmount);
         float r = Mathf.Pow(totalVol / ((4 / 3) * Mathf.PI), 1f/3f);
         sphereCollider.radius = r;
         radiousOfSpawn = r;

         //PROBLEM:

         float x = 0;
         float y = 0;
         float z = 0;
 
 
         for (int p = 0; p < protons.Length; p++)
         {
             for(int n = 0; n < neutrons.Length; n++)
             {
                 x += (protons[p].transform.localPosition.x + neutrons[n].transform.localPosition.x);
                 y += (protons[p].transform.localPosition.y + neutrons[n].transform.localPosition.y);
                 z += (protons[p].transform.localPosition.z + neutrons[n].transform.localPosition.z);
             }
         }
         float totalParticles = protonsAmount + neutronsAmount;
         x = x / totalParticles;
         y =  y / totalParticles;
         z =  z / totalParticles;
         sphereCollider.center = new Vector3(x, y, z);
     }

One thing that I can’t wrap my head around is how you are not getting an error comparing an int to a float in your loop. I imagine protonsAmount/neutronsAmount are floats, but you compare them to ints in your for loop without casting. I changed that to protons.Length and neutrons.Length.

float x = 0;
float y = 0;
float z = 0;

for (int p = 0; p < protonsAmount; p++)
{
    //x = 0; y = 0; z = 0;
    float xp = protons[p].transform.localPosition.x;
    float yp = protons[p].transform.localPosition.y;
    float zp = protons[p].transform.localPosition.z;
    x += xp; y += yp; z += zp;
}

for (int n = 0; n < neutronsAmount; n++)
{
    float xn = neutrons[n].transform.localPosition.x;
    float yn =  neutrons[n].transform.localPosition.y;
    float  zn = neutrons[n].transform.localPosition.z;
    x += xn; y += yn; z += zn;
}
float totalPar = neutronsAmount + protonsAmount;
x /= totalPar; y /= totalPar; z /= totalPar;

sphereCollider.center = new Vector3(x, y, z);