get var from script linked to instance using raycast

Hi,

i’m creating a menu for a game.
if the player comes close to an area, the game will list all items in that area as buttons on the screen.

for each of the buttons, it checks which items are in the List of that area and created 3dtext on top of the buttons so you know which button is which.

Now I’m trying to get my raycast to get that name of the button so I can add functionality to it.

The problem: I cannot access the itemName of the created instances with the raycast which I want to type out to the console (so i can use it later)


this is the list creating part:

Dictionary<string, List<string>> locItems = new Dictionary<string, List<string>>();

List<string> items = new List<string>();
List<string> itemsTwo = new List<string>();

this is the raycast script.

    void Update()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Input.GetButtonDown("Jump"))
    {
        if (Physics.Raycast(ray, out hit, 100))
        {
            //loop systeem.
            if (hit.collider.tag == "floor")
            {
                Debug.Log("You touched the floor!");
                Instantiate(walkTowards, hit.point, hit.transform.rotation);
            }
            if (hit.collider.tag == "ButtonPrefab")
            {
//THIS HERE IS THE PROBLEM!!
                string buttonName = hit.collider.GetComponent(typeof(ButtonBox).GetField(itemName) as string);
                Debug.Log(buttonName);
            }

I have this little piece of code linked to the buttonBox and buttonText prefabs (i’m creating 2 instances per button: 1 3dblock and 1 3dtext)

    void Awake()
{
    if (name != null)
    {
        name = itemLocation.Instance.itemName;

         //this is only on the buttonText.
        (gameObject.GetComponent(typeof(TextMesh)) as TextMesh).text = name;
    }

}

I’m using Oncollision to check the members of the 2 Lists and instantiating the buttons and 3dtext in there.

void OnTriggerEnter(Collider otherObject)
{
    //save name of object player collided with. 
    triggeredObject = otherObject.name;

    //maar waarom werkt dit? items heeft toch al iets erin?
    List<string> items = locItems[triggeredObject];
    //dit zou ik schrijven:
    //List<string> tempItems = new List<string>();
    
    //this is temp
    contents = "";

    //initial start position of buttons
    float posY = -3.1f;

    //create buttons & buttontext foreach string in list.
    foreach (string s in items)
    {

        //set Buttonblock position
        Vector3 positionItemButton = new Vector3(-9.5f, posY, -10);
        Vector3 rotationItemButton = new Vector3(90, 180, 0);
        //set Buttontext position
        Vector3 positionText = new Vector3(-12, posY + 0.8f, -10);
        Vector3 rotationText = new Vector3(180, 180, 180);
        //create button-block
        Instantiate(button, positionItemButton, Quaternion.Euler(rotationItemButton));
        
        //set public text to List contents, so prefab can use it.
        itemName = s;

        //create button-text
        Instantiate(buttonText, positionText, Quaternion.Euler(rotationText));

        //set Y pos so next button is placed correctly
        posY = posY - 1.2f;
    }

}

That looks like it should be:

 string buttonName = hit.collider.GetComponent<ButtonBox>().itemName;

Presuming itemName is the name of the field you want to access. Use the generic version of GetComponent() and GetField() is a reflection function that you weren’t calling right and don’t need to use anyway.