- Home /
Change value of dropdown using JS
I am having problems changing a value of a dropdown in a component. I can do things like disable/enable the component for example:
scriptName1.GetComponent("VisGameObjectPropertyModifier").enabled = false;
But I seem unable to change the value for a drop down list in that component. The Option is called 'Controller', and the values in the drop down are 'High', 'Medium', 'Low'.
I've tried all sorts of ways, but end up getting null references, unexpected tokens, and all sorts of other stuff.
Hope someone can give me a nudge in the right direction!
Thanks
Paul
When you say "Option", do you mean 'Controller' is a variable? If it is a variable inside your component, you just need to call it and set the new value. You take the "enabled" from that sentence and replaces with the variable name, and after the "=", you just set the new value. If not the case, you might want to post your code, so we can see how your 'Controller' is coded into your component.
Thanks for the reply Rodrigo! I can't post the script for the controller as it is an asset bought off the Unity Store (Visulaizer Studio).
I guess Controller is a variable as the options for 'High' etc are it's drop down.
I did try what you suggested before I posted as it seemed the most logical. Here it is: scriptName1.GetComponent("VisGameObjectProperty$$anonymous$$odifier").Controller = High;
But I get the error: BCE0005: $$anonymous$$ identifier: 'High'.
Answer by Seth-Bergman · Aug 05, 2012 at 03:37 PM
by "dropdown" I assume you mean enum. So, to change the value, you would say (depending on the name of the enum and the instance of said enum):
enum MyDropdown {high,medium,low}; // this is the enum
var Controller : MyDropdown; // this is the instance we see in the inspector
to set it, you would say:
Controller = MyDropdown.high;
to compare:
if (Controller == MyDropdown.low)
etc.. So basically you can check the script for the declaration of the var "Controller", and that will give you the name of the enum
Hi Seth! O$$anonymous$$, I need to go read up on enum (first I have ever heard of it!). It says 'Controller' in the inspector panel, so am I safe to assume that is it's real var value to target?
Thanks for the code examples. $$anonymous$$uch appreciated.
I ask about the var, as it's a component I bought from the Unity Store, and not something I wrote myself.
yes, it's safe to assume, although it's probably not capitalized now that I think about it.. to figure it out, add the line :
controller = "hello";
to the start function, or awake, or update..
this will throw an exception, which will tell you the name of the data type!
it will say:
cannot convert 'String' to 'NameOfEnum'
then, you know how to access it:
scriptName1.GetComponent("VisGameObjectProperty$$anonymous$$odifier").controller = NameOfEnum.High;
if lowercase doesn't work (unknown identifier 'controller') try uppercase
Controller = "hello";
Answer by code-blep · Aug 09, 2012 at 07:35 PM
For anyone else using the Visualizer Studio I found the following worked great when trying to change a different property (in this case the 'Target Property' dropdown) but it works just the same:
//Specify the game object in the inspector panel
var scriptName1 : GameObject;
//The targetProperty = 12; is the value for 'Uniform Scale' just count down the list (starting with 0) to find the correct value
scriptName1.GetComponent("VisGameObjectPropertyModifier").targetProperty = 12;
Hope it helps, and again thanks to Seth for putting me on the right track :)
Paul