GameObject.Find using a variable not working

Hi, i have a problem with GameObject.Find(text), this is not working it returns null, if i use it as normal like GameObject.Find(“text”), it works.
This is the code:

public void SearchFor()
{
    public string text="text";
    Camera.main.transform.position = GameObject.Find(text).transform.position;
    Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, 
    Camera.main.transform.position.y + 20, Camera.main.transform.position.z);
    Camera.main.transform.rotation = Quaternion.Euler(90, 0, 0);
}

Greetings @cozmadenis. A couple of things

I’ve tried this and had no problems using either a variable or a literal in GameObject.Find. Here’s some sample code I used and it worked OK for me.

using UnityEngine;

public class Test1 : MonoBehaviour
{
    private void Start()
    {
        string target = "Text";
        GameObject go = GameObject.Find(target);
        print(go.name);
    }
}

Secondly, I guess you’ve trimmed down your code for the sake of an example but you can’t use public inside a Method. In fact, you can’t use either public or private inside a Method. You can only create local variables. If I put the access modifier “public” in front of my “string target” statement, I get a compiler error. We might need to see more of your original to get hold of your problem.

If you have made the string public elsewhere (as a field variable at the top of the class), are you sure that you haven’t got a null string in the Inspector?

this is the whole code:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class Search : MonoBehaviour
{
public string text;

public void SearchFor()
{
    text = transform.GetChild(0).GetChild(0).GetChild(2).GetComponent<TextMeshProUGUI>().text;
    Camera.main.transform.position = GameObject.Find(text).transform.position;
    Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y + 20, Camera.main.transform.position.z);
    Camera.main.transform.rotation = Quaternion.Euler(90, 0, 0);
}

}
also i have a screenshot while debugging. The one called with variable gave me null, the one called with “Labels” works[201204-aaaa.png|201204]