OverlapSphere won't accept a variable as position (stays instead at 0,0,0?)

Hi everyone,

i have the same problem as @Elfrick here:

The fix he was told uses cartesian coordinates, but i like to retain the orbital arrangement and want to know what I did wrong.

I tried many things, but i cant get the Collider to accept the variable, it seems to use (0,0,0) instead of the new coordinates. It should be impossible to have a sphere in the middle of the Collider, too, but there it is. The code should check the space around each sphere and prevent other spheres to be created near another sphere.


My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Galaxy : MonoBehaviour
{
    // TODO: Have these values import from user settings
    public int numberOfStars = 300;
    public int maximumRadius = 100;

    public float minDistBetweenStars;

    // Start is called before the first frame update
    void Start()
    {
        int failCount = 0;

        for (int i = 0; i < numberOfStars; i++)
        {

            float distance = Random.Range(0, maximumRadius);
            float angle = Random.Range(0, 2 * Mathf.PI);

            var startPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
            // Vector3 startPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
            Collider[] positionCollider = Physics.OverlapSphere(startPosition, minDistBetweenStars);

            if (positionCollider.Length == 0)
            {
                GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = startPosition;
                failCount = 0;
            }
            else
            {
                i--;
                failCount++;
            }
            if (failCount > numberOfStars)
            {
                Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars is too big!");
                break;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

After changing the code to the one in the comments, i get this picture:

@xxmariofer, can you reupload the gif? The code you posted has the same problem, as there will be a circular void around the origin, not around othere spheres.

Interestingly, if you set the position of the OverlapSphere at the origin (0,0,0), other than the first object, you can’t generate other spheres, even if you set the Radius of OverlapSphere to 0. I believe it has something to do with GameObject.CreatePrimitive(PrimitiveType.Sphere), which will be created at the origin, too. But i can’t seem to find away to create it elsewhere, just transform.position it to somewhere else after the creation at the origin.

The situation with Overlapsphere does not make any sense at all. Even setting a static position like (10,0,10) as the position of the OverlapSphere with Vector3 will generate a void around the origin.