r/UnityHelp • • Aug 29 '21

PROGRAMMING How do I get a component's property as method parameter?

So I'm trying to change the Light component's Range property but I'm not sure what to put as its type. I tried to get it as string and then convert that to the Range property but I couldn't figure out how. Does anyone know how to do this?

4 Upvotes

6 comments sorted by

2

u/Bonejob Code Guru Aug 29 '21

the Range is a property of the light object so you would get an instance of the light and then reference it by {reference}.Range Don't forget to check if it is a Light type that supports range or it will error out.

//Modify Light Property
Light lightRef = GameObject.Find("Directional Light").GetComponent<Light>();
    if (lightRef.type == LightType.Spot) {
        lightRef.range = 25;
    }

1

u/Placeboy0 Aug 29 '21

my apologies I think I should've been clearer. Here's the code:

void GetProperty(float currentValue_, float min_, float max_, PropertyName property)

{

light_.property += changeSpeed * Time.deltaTime;

}

the light_ and changeSpeed variables are already defined, what I can't figure out is what to input in place of "PropertyName" because it's not working. thanks!

1

u/Bonejob Code Guru Aug 29 '21

Your original post says you were looking for the Range, is that what you are trying to modify? If that is right it would be "Range". Is light_ the object or a reference to the object?

1

u/Placeboy0 Aug 30 '21

light_ is the reference to the Light component. im trying to figure out how to reference a property of a component through a method’s parameters, basically. but no luck so far :/

1

u/Bonejob Code Guru Aug 30 '21

I am just not getting it. A property at the lowest level can not be referenced directly, you can only reference the object that has the property.

1

u/Placeboy0 Aug 30 '21

yeah that was my question basically, thanks for clearing that up. take care!!