- Home /
Link a value to another value in the Inspector
Hello I haven't find anything on this so I thought I'll give this a shot: I have a script (Transformer) which takes a float and alters it (with perlin noise, sinus, etc.). I put this script on a GameObject. How can I link let's say the Y rotation of the GO to this float in the script via Inspector? Thing is I don't always want to alter the Y rotation but maybe a light's intensity or a scale or anything else. so I need to be able to link values (floats, Vectors) dynamically, like I'm able to do with textures, gameobjects etc. etc by drag & drop.
Thanks in advance!
As far as I know you cannot link variables of a component to other variables of another component attached to the same object. While you can access those variables in the script, that is coded into the script and changing the variable dynamically is not something that is readily possible.
Answer by whydoidoit · Feb 20, 2014 at 05:03 AM
You could create a component that runs in Edit Mode ([ExecuteInEditMode]) that you use to make the links, there is nothing built in to do that but it could be a very useful script if you want to link things together in this way. If you used reflection to get and set the values then it could be quite flexible - the only problem being that changing just one part of a Vector3 would probably need a specific code path.
Create a class flagged with ExecuteInEditMode
Create two Component references for source and target
Create two string variables to describe the value to read and the value to set
Use reflection to get the FieldInfo for the source and target properties on the objects
Check for changes in the Update function of the new class and apply changes
Clearly this would be simple if you just linked a direct value - the code path for a Vector3 involves reading the vector, updating the component and writing it back again - this happens with anything that is a struct (value type).
Thanks a lot for the tip! I'll check it out once I have the time. If it's complicated to alter only 1 value of a Vector3, I'll pas the whole vector :) I'll check out this whole "reflection" and "ExecuteInEdit$$anonymous$$ode" deal. If I find out, this is what I'm looking for, you'll have my gratitude (and the karma) :)
Yep, that solved the problem. Though I thought I would rather make it in the conventional way. Reflecting several dozen values per frame, I imagine, could be much more detrimental than running more lines of code in a dozen instances of a script :) Thanks anyways! $$anonymous$$udos!
Yep reflection isn't fast. Ok during setup but best to limit it per frame.
I ended up making 1 base class that handled a float modification by various modes and other classes that are extended to this class and use its functions. One class for every aspect I want to change (position, rotation, scale, material color, light color/intensity, etc.) Only I have a little concern: is accessing a custom value of a material like GetValue("_Color") SetValue("_Color") the same as reflection? I mean I access it by a string... I reckon it might be as slow as reflections...
Your answer
Follow this Question
Related Questions
NullRefenceException Using Arrays 2 Answers
Floating-point errors while assigning transforms 1 Answer
Can't assign dictionary values 0 Answers
Unity4 Ignoring Inspector/Initialized Public Value in Build 1 Answer
Arrays and the inspector 1 Answer