Unity Script Breakdown: Change Color and Number on Game Object
This article provides a detailed breakdown of a Unity C# script, explaining its purpose, structure, and functionality. The script primarily handles changing the color of a GameObject's material and updating text components with a numerical value.
Purpose of the Script
The script named `tcg_change_color_number` is designed to:
1. Change the material color of a GameObject.
2. Update text components of a child GameObject with a given number.
3. Allow dynamic updates to the color and number through a public method.
Script Breakdown
1. Importing Necessary Namespaces
```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
```
These lines import essential namespaces required for the script:
- `System.Collections` and `System.Collections.Generic` are used for collections and lists.
- `UnityEngine` is necessary for Unity-specific functionalities.
- `UnityEngine.UI` is used for UI components such as `Text`.
2. Defining the `ColorNumberData` Class
```csharp
[System.Serializable]
public class ColorNumberData
{
public Color color = new Color(1f, 0.5f, 0f); // RGB values for orange (R=1, G=0.5, B=0)
public int number = 1;
}
```
The `ColorNumberData` class is marked as `Serializable`, allowing its instances to be shown and edited in the Unity Inspector. This class holds:
- `color`: A `Color` object initialized to orange (RGB: 1, 0.5, 0).
- `number`: An integer initialized to 1.
3. Defining the Main Script `tcg_change_color_number`
```csharp
public class tcg_change_color_number : MonoBehaviour
{
[SerializeField]
private ColorNumberData data;
[SerializeField]
private GameObject text_child_gameobject;
private void Awake()
{
// Accessing the color and number values
Debug.Log("Color: " + data.color);
Debug.Log("Number: " + data.number);
change_Mat_color(data.color);
SetGameObjectText(text_child_gameobject, data.number);
}
```
- `data`: An instance of `ColorNumberData`, serialized to appear in the Inspector.
- `text_child_gameobject`: A `GameObject` that holds the text components to be updated, serialized for Inspector visibility.
- `Awake()`: A Unity event method called when the script instance is being loaded. It logs the initial color and number, changes the material color, and sets the text components' values.
4. Public Method to Change Data
```csharp
public void change_data(Color color, int number)
{
Debug.Log("jlk" + color + "adsafsd" + number);
change_Mat_color(color);
SetGameObjectText(text_child_gameobject, number);
}
```
This method allows dynamic updates to the color and number:
- Logs the new color and number.
- Calls `change_Mat_color()` to update the material color.
- Calls `SetGameObjectText()` to update the text components.
5. Changing Material Color
```csharp
public MeshRenderer meshRenderer;
public void change_Mat_color(Color c)
{
if (meshRenderer == null) {
meshRenderer = GetComponent<MeshRenderer>();
}
if (meshRenderer != null)
{
// Set the material color to the provided color in the serialized field
meshRenderer.material.color = c;
}
else
{
Debug.LogError("MeshRenderer component not found on the GameObject.");
}
}
```
- `meshRenderer`: A reference to the `MeshRenderer` component.
- `change_Mat_color(Color c)`: Updates the material color of the GameObject. It checks if `meshRenderer` is null and tries to get the `MeshRenderer` component. If found, it changes the material color; otherwise, logs an error.
6. Updating Text Components
```csharp
public void SetGameObjectText(GameObject parentObject, int number)
{
// Get all the children of the parentObject
Text[] childrenTextComponents = parentObject.GetComponentsInChildren<Text>();
// Modify the text value for each child Text component
foreach (Text textComponent in childrenTextComponents)
{
Debug.Log(textComponent.gameObject.name+"here"+number);
textComponent.text = ""+number;
}
}
```
This method updates the text components of the specified `parentObject`:
- Retrieves all `Text` components in the children of `parentObject`.
- Iterates through each `Text` component and sets its text to the provided number.
- Logs the name of each text component and the new number.
7. Commented Out Version of `SetGameObjectText`
```csharp
/*
public void SetGameObjectText(GameObject obj, int number)
{
Text textComponent = obj.GetComponent<Text>();
if (textComponent != null)
{
// Set the text value to the provided value in the serialized field
textComponent.text = ""+number;
}
else
{
Debug.LogError("Text component not found on the GameObject.");
}
}
*/
```
This is an alternative version of `SetGameObjectText`, commented out, which only updates a single `Text` component on the `GameObject` directly passed to it.
Conclusion
This Unity script effectively manages the color and numerical text display of a GameObject. By understanding each part of the script, you can modify or extend its functionality to suit your project's needs. The use of serialized fields, Unity event methods, and component interactions are key techniques demonstrated in this script.