i ahve that voids i wanna have tmproo text cvaraible component there must be in text…
- User
i ahve that voids
i wanna have tmproo text cvaraible component there must be in text field on of 3 states string
i ahve scriptt of spawnpoint
it can be ready to activate, or be locekd, or be already unlocked and reached
also i wanna have public Rendere component and get it on trhigge enterr
also there i need 3 public varibles for 3 colors of each string state , there must be diffrenet tmpp text color and different color on rendererr on trigge enterr
i mean for example locked triggerr must have red text red renderre material, also use for its MaterialProperties block
public string Accept= "Активен"; public string Ready= "Готов"; public string Close= "Закрыт"; private void OnTriggerEnter(Collider other) { ManageParkourPoint mainManager = FindObjectOfType<ManageParkourPoint>(); if (mainManager.spawnPoints.IndexOf(this) == mainManager.currentSpawnIndex) { isNextPoint = true; enterNextPointEvent.Invoke(); } else { enterInactivePointEvent.Invoke(); } } - Assistant
Certainly! I'll demonstrate how you can structure your script to handle the three states for your
TextMeshProcomponent andRenderercomponent upon trigger entry. Additionally, I'll show how to assign different colors for each state using Material Properties Block.Here's how the enhanced script could look in Unity C#:
using UnityEngine; using TMPro; public class SpawnPoint : MonoBehaviour { public string Accept = "Активен"; public string Ready = "Готов"; public string Close = "Закрыт"; public TextMeshPro textComponent; // Link to the TextMeshPro component public Renderer rendererComponent; // Link to the Renderer component public Color acceptColor = Color.green; public Color readyColor = Color.yellow; public Color closeColor = Color.red; private void Start() { // Assuming these components are attached to the same GameObject if (textComponent == null) textComponent = GetComponent<TextMeshPro>(); if (rendererComponent == null) rendererComponent = GetComponent<Renderer>(); } private void OnTriggerEnter(Collider other) { ManageParkourPoint mainManager = FindObjectOfType<ManageParkourPoint>(); bool isCurrent = mainManager.spawnPoints.IndexOf(this) == mainManager.currentSpawnIndex; // Determine the state if (isNextPoint) { UpdateState(Accept, acceptColor); enterNextPointEvent.Invoke(); } else // assuming other scenarios like being locked can be added here { enterInactivePointEvent.Invoke(); } } private void UpdateState(string stateText, Color color) { // Update TMPro text if (textComponent != null) { textComponent.text = stateText; textComponent.color = color; } // Update Renderer color using Material Property Block if (rendererComponent != null) { MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock(); propertyBlock.SetColor("_Color", color); rendererComponent.SetPropertyBlock(propertyBlock); } } }Explanation:
-
Variables:
- We declare public strings for the three states:
Accept,Ready, andClose. textComponentis a reference to theTextMeshProtext component.rendererComponentis a reference to theRenderercomponent.- We have colors for each state:
acceptColor,readyColor, andcloseColor.
- We declare public strings for the three states:
-
Start Method:
- Ensures the
TextMeshProandRenderercomponents are assigned. If not, it tries to find them on the same GameObject.
- Ensures the
-
OnTriggerEnter Method:
- It finds the main manager and checks if the current spawn point is the next one in the sequence.
- Depending on whether it is the correct spawn point and other conditions (you can elaborate further based on your needs), it updates the state text and colors.
-
UpdateState Method:
- Takes the state text and color as parameters.
- It updates the
TextMeshProtext and its color. - It also updates the renderer's material color using a
MaterialPropertyBlock, which ensures each shared material instance gets its unique color.
You can customize this structure further based on additional state transitions and conditions. Make sure the
TextMeshProandRenderercomponents are correctly assigned, either via the Inspector or dynamically as shown. -