Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity UI extended components, other script cannot get the reference of these component?

Discussion in 'UGUI & TextMesh Pro' started by cdytoby, Oct 7, 2016.

  1. cdytoby

    cdytoby

    Joined:
    Nov 19, 2014
    Posts:
    181
    I created a ScrollRectEx, which is a extended class from ScrollRect according to the scripts on Internet. And I added some custom values.

    Then I created another monobehaviour script, and try to get the ScrollRectEx component, it's ALWAYS null. Both components are on the same GameObject.

    I tried "GetComponent()", it's null. I also tried using public and [SerializeField] on the ScrollRectEx field, and assign this field with that component by drag&drop, it's still null.

    What is going on?
     
  2. mikael_juhala

    mikael_juhala

    Joined:
    Mar 9, 2015
    Posts:
    247
    You can definitely extend Unity UI's components and get references to them. We're doing it all the time.

    Could you show the code you're using to get the reference? Is the script file containing ScrollRectEx also called ScrollRectEx.cs?
     
  3. cdytoby

    cdytoby

    Joined:
    Nov 19, 2014
    Posts:
    181
    It was my mistake, I have another game object which have this script on it, and the error message is from the other game object.

    case closed.
     
  4. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    227
  5. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    294
    You also need to provide an Editor for your extended Slider, otherwise the default editor is used (it doesn't draw the fields you added). Take a look at the Slider editor: here
     
    NGC6543 likes this.
  6. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    227
    I really appreciate your kind help. Have a nice day!