Search Unity

Accessing subcomponent of instance prefab

Discussion in 'UGUI & TextMesh Pro' started by Thor-Apps, May 7, 2016.

  1. Thor-Apps

    Thor-Apps

    Joined:
    Dec 13, 2013
    Posts:
    32
    Hi,
    I want to change the text on a sub-component of an instanced prefab.
    I do:

    1. GameObject levelItemInstance;

    2. levelItemInstance = Instantiate(levelItem);
    3. levelItemInstance.transform.localScale = new Vector3(1f, 1f, 0);

    4. levelItemInstance.transform.Find("levelName").GetComponent<Text>().text = "WWWW";

    levelItem has many childs and those also has childs (one of them is levelName). I'm doing correctly?

    I got this error (on line 6):
    1. NullReferenceException: Object reference not set to an instance of an object

    Thanks for your help
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Thor-Apps

    Thor-Apps

    Joined:
    Dec 13, 2013
    Posts:
    32