Calculate the radius of a spherical game object

I want to calculate the radius of my planet. I am using raycasting to detect when my line between ships hits a planet and then put raycast info into a RaycastHit object.
I know RaycastHit has property called collider and also Rigidbody object attached to the collider if any.
I could possibly use this line:

planetRadius = Vector3.Distance(rayHit.collider.bounds.center, rayHit.collider.bounds.extents);

I also tried this:

((SphereCollider)rayHit.collider).radius;

but this just prints 0.5 since the sphere collider is 0.5 in the inspector and this value is the same for all my spheres whether or not they have same size.

I am not sure if this is the right way to do this. I could detect the game object I hit by the tag of the object and then access the Mesh Renderer for info.

What can you recommend me to do, what is the best way to get the radius of the Mesh renderer inside the sphere?

You can access the MeshFilter’s mesh or sharedMesh variable, and then acces the vertices. Pick any one of those, they are stored as Vector3s. Since it is a sphere, all vertices will be the same distance from the center, so you only need to calculate that.

Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
float radius = Vector3.Distance(transform.position, vertices[0];

Of course, this only works if the origin of the GameObject is at the sphere’s center (which is the case with the primitive sphere mesh).

I didn’t test it, but I guess you can use the bounding box of the planet to get the radius:

Mesh mesh = GetComponent<MeshFilter>().mesh;
Bounds bounds = mesh.bounds;
float radius = bounds.extents.x; // or .y or .z ... should be the same in all directions