how to save radius attached to game object (CircleCollider2D)

I’m trying to save the radius of the circlecollider2d attached to the ship (the gameobject) instead of retrieving that information every time to wrap. I’m declaring a field in the script to store the value. Basically, how do I retrieve the CircleCollider2D component and save its radius into the field?

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.UIElements;
///<summary>
/// This is a ship script.
///</summary>
public class Ship : MonoBehaviour
{
    #region
    private Rigidbody2D ShipRigidbody2D;
    private CircleCollider2D ShipCircleCollider2D;
    #endregion
    #region
    const float Radius = 2;
// Private member Ship.Radius is unused.
    #endregion 
    void Start()
    {
        ShipRigidbody2D = GetComponent<Rigidbody2D>();
        ShipCircleCollider2D = GetComponent<CircleCollider2D>();
// Private member Ship.ShipCircleCollider2D can be removed as the value assigned to it is never read.

    }
   
    #region
    Vector2 vel = new Vector2(1, 0);
    #endregion
    const float ThrustForce = 5;
    void FixedUpdate()
    {
        float space = Input.GetAxis("Thrust");
        space *= ThrustForce;
        vel.x = space;
        ShipRigidbody2D.velocity = vel;
    }
    void Update()
    {
       
    }
   
}

Forgot but I needed to declare the radius to store it. The code that I will use is const float Radius = 2;

You won’d be able to save anything to a const variable. But there should be a property on the CircleCollider2D called radius:

I’ve updated the code to

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.UIElements;
///<summary>
/// This is a ship script.
///</summary>
public class Ship : MonoBehaviour
{
    #region
    private Rigidbody2D ShipRigidbody2D;
    #endregion
    #region
    private CircleCollider2D Col;
    #endregion
    #region
    public float radius;
    #endregion
    void Start()
    {
        ShipRigidbody2D = GetComponent<Rigidbody2D>();
      
        Col = gameObject.GetComponent<CircleCollider2D>();
        radius = Col.radius;
    }
  
    #region
    Vector2 vel = new Vector2(1, 0);
    #endregion
    const float ThrustForce = 5;
    void FixedUpdate()
    {
        float space = Input.GetAxis("Thrust");
        space *= ThrustForce;
        vel.x = space;
        ShipRigidbody2D.velocity = vel;
    }
    void Update()
    {
      
    }
  
}

The message in the console disappears so I think that this should store the radius and call the CircleCollider2D component. Correct me if I’m wrong

Is this for performance?!! You can freely retrieve the .radius property every frame. This is infinitely preferrable to all that extra code storing it, retrieving it, what if someone modifies it (it’s public!), then it doesn’t actually change anything, etc.

Do not optimize until you have measured and identified (quantified!) the precise source. You’re just wasting time if you are, and mucking up your codebase with extra cruft. Keep it simple. Seriously.

1 Like

That isn’t my intention. I’m new to unity and c# so I’m beating around the bush basically.

Jump in with both feet then, you’ll soon see the problem with writing extra lines of unnecessary code.

Every single character of code you put in can have a bug in it.

The fewer you put in, the lower the chances of a bug.

Let me leave you with a famous quote:

“One of my most productive days was throwing away 1000 lines of code.”

The internet argues who said it. I’ll let you google.