Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question I have a mistake that says that it doesn't see the text I tried everything and Idk what to do :(

Discussion in 'Scripting' started by retunskijmihail071, Apr 13, 2024.

  1. retunskijmihail071

    retunskijmihail071

    Joined:
    Apr 13, 2024
    Posts:
    2
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    public class GateGenerator : MonoBehaviour
    {
    public GameObject goodGatePrefab;
    public GameObject badGatePrefab;
    public TextMeshPro goodGateText;
    public TextMeshPro badGateText;
    public int numberOfRows = 5;
    public int pairsPerRow = 3;
    public float gapBetweenPairs = 2f;
    public float gapBetweenRows = 2f;
    [SerializeField] float multiplicationGateProbability = 0.3f;
    [SerializeField] float divisionGateProbability = 0.3f;
    [SerializeField] private TextRandomizer tr;
    void Awake()
    {
    tr.GetComponent<TextRandomizer>();
    GameObject goodPanel = goodGatePrefab.transform.Find("Cube").gameObject;
    GameObject badPanel = badGatePrefab.transform.Find("Cube").gameObject;
    Canvas goodCanvas = goodPanel.GetComponentInChildren<Canvas>();
    Canvas badCanvas = badPanel.GetComponentInChildren<Canvas>();
    goodGateText = goodCanvas.GetComponentInChildren<TextMeshPro>();
    badGateText = badCanvas.GetComponentInChildren<TextMeshPro>();
    }
    void Start()
    {
    GenerateRows();
    }
    void GenerateRows()
    {
    Vector3 spawnPosition = transform.position;
    for (int i = 0; i < numberOfRows; i++)
    {
    SpawnRow(spawnPosition);
    spawnPosition.z += gapBetweenRows;
    }
    }
    void SpawnRow(Vector3 spawnPosition)
    {
    Vector3 pairSpawnPosition = spawnPosition;
    for (int i = 0; i < pairsPerRow; i++)
    {
    SpawnPair(pairSpawnPosition);
    pairSpawnPosition.x += gapBetweenPairs * 3;
    }
    }
    void SpawnPair(Vector3 pairSpawnPosition)
    {

    GameObject firstGate = Random.Range(0f, 1f) < 0.5f ? goodGatePrefab : badGatePrefab;
    if (firstGate == goodGatePrefab && Random.Range(0f, 1f) < multiplicationGateProbability)
    {
    tr.MultiplyRandom();
    int MultiplyingVar = tr.mr;

    goodGateText.text = "x" + MultiplyingVar.ToString();
    Instantiate(goodGatePrefab, pairSpawnPosition, Quaternion.identity, transform);
    }
    else if (firstGate != goodGatePrefab && Random.Range(0f, 1f) < divisionGateProbability)
    {
    tr.DivideRandom();
    int dividingVar = tr.dr;

    badGateText.text = "÷" + dividingVar.ToString();
    Instantiate(badGatePrefab, pairSpawnPosition, Quaternion.identity, transform);
    }
    else
    {
    // Instantiate the chosen gate
    Instantiate(firstGate, pairSpawnPosition, Quaternion.identity, transform);
    }
    pairSpawnPosition.x += gapBetweenPairs;

    GameObject secondGate = Random.Range(0f, 1f) < 0.5f ? goodGatePrefab : badGatePrefab;
    if (secondGate == goodGatePrefab && Random.Range(0f, 1f) < multiplicationGateProbability)
    {
    tr.MultiplyRandom();
    int MultiplyingVar = tr.mr;
    goodGateText.text = "x" + MultiplyingVar.ToString();
    Instantiate(goodGatePrefab, pairSpawnPosition, Quaternion.identity, transform);
    }
    else if (secondGate != goodGatePrefab && Random.Range(0f, 1f) < divisionGateProbability)
    {
    tr.DivideRandom();
    int dividingVar = tr.dr;

    badGateText.text = "÷" + dividingVar.ToString();
    Instantiate(badGatePrefab, pairSpawnPosition, Quaternion.identity, transform);
    }
    else
    {
    Instantiate(secondGate, pairSpawnPosition, Quaternion.identity, transform);
    }
    }
    }
     
  2. retunskijmihail071

    retunskijmihail071

    Joined:
    Apr 13, 2024
    Posts:
    2
    My gate prefab has a cube on it which has a canvas which has text:
     

    Attached Files:

    • 1.png
      1.png
      File size:
      8.1 KB
      Views:
      11
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Please use the code tags.
    You have:
    Code (csharp):
    1.  
    2. tr.GetComponent<TextRandomizer>();
    3.  
    It doesn't do anything. Should it be?
    Code (csharp):
    1.  
    2. tr = GetComponent<TextRandomizer>();
    3.  
     
    samana1407 likes this.
  4. ivanroblx20148358235

    ivanroblx20148358235

    Joined:
    Mar 25, 2024
    Posts:
    1
    Speaking of text, when I go closer to TMP text it disappears.
     
  5. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Decrease your Camera near clip plane