Home > Enterprise >  Getting object name and randomly placing it to Text UI - Unity
Getting object name and randomly placing it to Text UI - Unity

Time:10-08

I am a beginner in Unity and I am currently making a simple game. I have a problem managing the flow of my minigame. The minigame is simply finding an object, when I found and tap on the item name below will be shaded or marked or there can be animation just to indicate that the item is found.

What I want to do is to get the name of the objects that need to be found and set them randomly in the three (3) item names below. like every time this minigame opens the names of the items are randomly placed in the 3 texts. And when the item is found the name below will be marked or shaded or anything that will indicate it is found, but for now I will just set it inactive for easier indication. How can I properly do this whole process?

The objects inside the scene are button for them to have onCLick() events

Correction: the term choices are wrong because they just display the name of the items that you need to find, just in case you get confused with the term choice in my minigame. I will fix it.

Here is the visuals for the minigame: enter image description here

enter image description here

The script I currently have for when the objects was clicked:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;

public class ClickObject : MonoBehaviour
{
    [SerializeField] Button pillowBtn, pcBtn, lampBtn;
    // [SerializeField] TextMeshProUGUI choice1, choice2, choice3;
    
    
    // FROM THE SOLUTION
    [SerializeField] List<GameObject> gameObjectWanted;
    [SerializeField] List<TextMeshProUGUI> textBoxes;

    // METHOD NAMES IS TEMPORARY
    public void pillowClicked()
    {
        Debug.Log("you found the "   EventSystem.current.currentSelectedGameObject.name);
    }

    public void desktopClicked()
    {
        Debug.Log("you found the "   EventSystem.current.currentSelectedGameObject.name);
    }

    public void lampClicked()
    {
        Debug.Log("you found the "   EventSystem.current.currentSelectedGameObject.name);
    }
}

CodePudding user response:

You asked for a lot and I hope I understood your intention. first, if you want to randomly choose an amount of game objects from your game, I think the best way to do it is by adding the refernece of all the game objects you want to choose from radomly inside a list of game objects and then randomly take a game object of the list and make it a child of another game object I call "fatherGoTranform" on my code like that:

[SerializeField] List<GameObject> gameObjectWanted;
[SerializeField] float numOfGO = 4;
[SerializeField] Transform fatherGoTranform;

void Start()
{
  for(int i=0;i<numOfGO;i  )
  {
    int index = Random.Range(0, gameObjectWanted.Count-1);
    GameObject currentGO = gameObjectWanted[index ];
    currentGO.transform.parent = fatherGoTranform;
    gameObjectWanted.RemoveAt(index);
  }
}

and then to click on a game object and the do with what you want try this:

void Update()
  {
     //Check for mouse click 
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit raycastHit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out raycastHit, 100f))
         {
             if (raycastHit.transform != null)
             {
                //Our custom method. 
                 CurrentClickedGameObject(raycastHit.transform.gameObject);
             }
         }
     }
}

I have not checked the code so if there is an error tell me and I will fix it

CodePudding user response:

This is the solution that worked for my problem. I randomly placed numbers to the list according to childCount and every index indicate the index of the text that I want to put them on which I get as a Transform child.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;

public class ClickObject : MonoBehaviour
{
    // [SerializeField] Button pillowBtn, pcBtn, lampBtn;
    [SerializeField] GameObject choices, v1, finishPanel;

    // RANDOM NUMBER HOLDER FOR CHECKING
    private int randomNumber, foundCount;
    // RANDOMIZED NUMBER LIST HOLDER
    public List<int> RndmList = new List<int>();

    private void Awake()
    {
        foundCount = 0;
        RndmList = new List<int>(new int[v1.transform.childCount]);

        for (int i = 0; i < v1.transform.childCount; i  )
        {
            randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount)   1);
            while (RndmList.Contains(randomNumber))
            {
                randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount)   1);
            }
            RndmList[i] = randomNumber;
            // Debug.Log(v1.transform.GetChild(randomNumber-1).name);
            choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text = v1.transform.GetChild(randomNumber - 1).name;
        }
    }

    public void objectFound()
    {
        string objectName = EventSystem.current.currentSelectedGameObject.name;
        Debug.Log("you found the "   objectName);

        for (int i = 0; i < choices.transform.childCount; i  )
        {
            if (objectName == choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text)
            {
                // Debug.Log(i);
                // choices.transform.GetChild(i).gameObject.SetActive(false);
                // choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;

                if(RndmList.Contains(i 1))
                {
                    // Debug.Log(i);
                    // Debug.Log(v1.transform.GetChild(RndmList[i]-1).name);
                    choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;
                    v1.transform.GetChild(RndmList[i]-1).GetComponent<Button>().enabled = false;
                    foundCount  ;
                }
            }
        }

        if(foundCount == v1.transform.childCount)
        {
            finishPanel.SetActive(true);
        }
    }
}
  • Related