The C# scripts that you create from within the Assets folder become components to a GameObject when you drag them into it. Let’s say we have this script called Circle.cs that can get the circumference and diameter from its attribute, _radius:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Circle : MonoBehaviour
{
[SerializeField]
private float _radius = 0f;// Start is called before the first frame update
void Start()
{
Debug.Log($"Circumference of {_radius}: " + GetCircumference());
Debug.Log($"Diameter of {_radius}: " + GetDiameter());
}// Update is called once per frame
void Update()
{
}
float GetCircumference()
{
return 2 * 3.14f * _radius;
}
float GetDiameter()
{
return _radius * 2;
}
}
I’ll place this in the GameObject and in the inspector specify that the Radius is 10. Then, I’ll click Play.
You can see from the Console log above that the Start() method was called and it displayed the computation of the circumference and diameter of a Circle given the radius.
What if I had another script called Display.cs and its purpose is to display the radius, circumference and diameter on Text fields Radius_Text, Circumference_Text and Diameter_Text. The current script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Display : MonoBehaviour
{
[SerializeField]
private Text _radiusText;
[SerializeField]
private Text _circumferenceText;
[SerializeField]
private Text _diameterText;// Start is called before the first frame update
void Start()
{
//Display circle details in text UI
}// Update is called once per frame
void Update()
{
}
}
Then, I’ll drag the script on the GameObject and map all the Text components.
To be able to access the component Circle from Display, you can simple use the command GetComponent<Component>() and qualify the component that you want to obtain within angle brackets, <>. Do note that it’s best to null check before you attempt to use the component to eliminate run-time errors:
Display.cs
void Start()
{
//Display circle details in text UI
Circle circle = GetComponent<Circle>();
if (circle != null)
{
_radiusText.text = "Radius: " + _radius ;
_circumferenceText.text = "Circumference: " + circle.GetCircumference();
_diameterText.text = "Diameter: " + circle.GetDiameter();
}
}
However, even if you’re able to get the Circle component, there were some issues on grabbing its data and it’s due to access scopes:
To fix this, we will just need to make GetCircumference() and GetDiameter() publicly available outside the class with the public keyword and introduce another public method to get the Radius:
Circle.cs
public class Circle : MonoBehaviour
{
/* REDACTED */
public float GetCircumference()
{
return 2 * 3.14f * _radius;
}
public float GetDiameter()
{
return _radius * 2;
}
public float GetRadius()
{
return _radius;
}
}
Then, update the Start script of Display component to retrieve the radius from the public method:
void Start()
{
//Display circle details in text UI
Circle circle = GetComponent<Circle>();
if (circle != null)
{
_radiusText.text = "Radius: " + circle.GetRadius() ;
_circumferenceText.text = "Circumference: " + circle.GetCircumference();
_diameterText.text = "Diameter: " + circle.GetDiameter();
}
}
We can confirm that Display component was able to access data from Circle component by pressing Play.