1

I'm instanciating buttons via script and need then to call a script in a parent object a few levels up

unity hierarchy

This is in the prefab script and gets called when one of the buttons gets clicked. Performance isn't relevant at all, so I just told it to go from the bottom up until it reaches the controller game object that has the mllhildTestController.cs script attached.

public void ClickMe()
{
    string parentNode = ReturnLabel();
    string path = this.GetComponentInParent<mllhildTestController>().path;
    this.GetComponentInParent<mllhildTestController>().Clear();
    this.GetComponentInParent<mllhildTestController>().Load(path, parentNode);
}

but it only results in an error

NullReferenceException: Object reference not set to an instance of an object

this.something is working fine, so it has to be an error in my logic with the GetComponentInParent<mllhildTestController>() part.

Could someone please help me?

EDIT: this function seems to work fine, but since it was asked

public string ReturnLabel()
{
    return buttonText.text;
}

Controller script

public class mllhildTestController : MonoBehaviour
{
    public mllhildTestLinq linq;
    public mllhildTestDisplay display;
    public mllhildTestButtons buttons;
    public List<GameObject> listToStuff;

    public string test = "smName";
    public string smName = "mllhild";

    public string displayText = "display.textWindow.font";
    public string path = @"C:\SlaveMaker4\Slaves\";

    // Start is called before the first frame update
    void Start()
    {
        ClearText();

        // linq.LinqTest(@"C:\SlaveMaker4\Rhia.xml");
        // string filename = @"C:\SlaveMaker4\Rhia.xml";
        // linq.QueryXML(filename, parentNode);

        // Debug.Log(this.GetType().GetField("test").GetValue(this));
        // Debug.Log(this.GetType().GetField(test).GetValue(this));
        // Debug.Log(display.textWindow.font);
        // Debug.Log(this.GetType().GetField("display").GetType().GetField("textWindow").GetType().GetField("font").GetValue(this));
        // Debug.Log(typeof(TextMeshProUGUI).GetProperty(displayText).GetValue(this));
        // Debug.Log(this.GetType().GetField(displayText).GetValue(this));
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void SetText(string text)
    {
        display.textWindow.text = text;
    }

    public void AddText(string text)
    {
        display.textWindow.text += text;
    }

    public void ClearText()
    {
        display.textWindow.text = null;
    }

    public GameObject bfield;

    public GameObject AddNewButtonList(string label)
    {
         GameObject b = Instantiate(bfield) as GameObject;
         b.SetActive(true);
         b.GetComponent<PrefabListButtoms>().title.text = label;
         b.transform.SetParent(bfield.transform.parent, false);
         return b;
    }

    public void Clear()
    {
        foreach(GameObject b in listToStuff)
        {
            Destroy(b);
        }

        listToStuff.Clear();
    }

    public void LoadButton() { Load(path, "Language" ); }

    public void Load(string path, string parentNode)
    {
        string[] fileArray = Directory.GetFiles(path, "*.xml");

        foreach (string xmlfile in fileArray)
        {
            GameObject blist = AddNewButtonList(xmlfile + "\n" + parentNode);
            listToStuff.Add(blist);
            //AddText("\n\n" + xmlfile + "\n");
            //AddText("Parent-Node:" + parentNode + "\n");
            //AddText("Child-Nodes:" + "\n");

            linq.QueryXML(xmlfile, parentNode, blist);
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mllhild
  • 67
  • 7
  • could you show ReturnLabel() code? and mllhildTestController is a class of the script you would access? – Frenchy May 14 '20 at 11:54
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – pinkfloydx33 May 14 '20 at 12:03
  • to Frenchy, added the scripts to pinkflowydx33, sadly no – mllhild May 14 '20 at 12:08
  • To finalize the answer, Please remember to accept and vote up the answer if your original issue has been solved and then ask a new question if you have another issue: https://stackoverflow.com/help/someone-answers – Frenchy May 15 '20 at 18:18

1 Answers1

2

You cant access directly because you have to find parent and then you find the desired children

so your first challenge is to find the parent of your component:

so first command is : GameObject.Find("NameofObj") or GameObject.FindWithTag("NameOfTag")

so after you just find the desired children component from this GameObject..and finally you could access to the method of script linked to child

Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • 1
    Exactly. First find the UIController GameObject, and then select the "Controller" child from it. – Kuruchy May 14 '20 at 12:41
  • I ended up using FindObjectOfType().AddValueVar01(1); since there was only one controll script. this solved my problem in the end https://youtu.be/ymq2AUckws0 – mllhild May 18 '20 at 02:01