Search Unity

Script inheritance from Image preventing public variables from showing in Inspector

Discussion in 'UGUI & TextMesh Pro' started by Tetheta, Sep 15, 2014.

  1. Tetheta

    Tetheta

    Joined:
    Jan 24, 2014
    Posts:
    4
    I haven't been able to find an answer via searching, so does anyone know if there's a way to assign public variables from a script if said script is also an Image? Or perhaps just an easier way to mess with rectTransform besides inheriting from Image? I have two objects I want to give to the script to find their position difference but I can't see anything besides basic image functionality in the inspector.

    Code and inspector views are below:
     
  2. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    I think you'll have to write a custom editor.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    In the more general case, yes, you'll have to write a custom editor.

    However, why are you making this inherit from Image? Make it a normal MonoBheaviour; it doesn't seem to need anything that Image provides.
     
  4. Tetheta

    Tetheta

    Joined:
    Jan 24, 2014
    Posts:
    4
    I found another way, using static variables in a class, but the reason I'm inheriting is because I can't seem to use rectTransform without inheriting, do you know what I need to include/inherit to be able to use that?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can either GetComponent<RectTransform>(), or you can typecast the transform property like this: RectTransform rectTransform = (RectTransform)transform;
     
  6. Tetheta

    Tetheta

    Joined:
    Jan 24, 2014
    Posts:
    4
    Very useful to know, many thanks! I thought about typecasting it but I didn't think that work. I guess since I was only using the stuff RectTransform inherits from Transform it would've worked perfectly fine. GetComponent didn't even occur to me for some reason, still a lot to learn. Glad I'll be able to deal with this issue in the future though.