Putting a variable into a function

Hey

This is seem like a weird question but is something like this possible.

function Start () {

     ChangeVar(transform.position.x,5.5);
}

function ChangeVar(var1 : ?, num1 : float) {

     var1 = num1; //it would then set x position to 5.5
}

I know that will make no sense on its own and I could just do transform.position.x = 5.5, but I think I need to do something like this for something more complicated in a custom inspector script.

Regarding the main point of your question, in C# you could do that with a ref parameter, which actually allows you to pass a variable as a reference instead than as a value, but in UnityScript I don’t think that’s possible.

Alternately, following your other comments, you could use a lambda as a setter. I think it can be done in UnityScript, though I don’t know the correct syntax, so here is a C# example which you’ll need to convert:

DrawColorField(value => target.night.nightSky = value, target.night.nightSky, value => target.night.nightSun = value, target.night.nightSun);
void DrawColorField(Action<Color> colorSetter0, Color colorVal0, Action<Color> colorSetter1, Color colorVal1) {
  colorSetter0 = EditorGUILayout.ColorField("Sky Color:",colorVal0);
  colorSetter1 = EditorGUILayout.ColorField("Sky Color:", colorVal1);
}

As you see though, you’ll have to pass the value as a separate parameter, since the lambda is used only to set the field and not to return it’s value. So it’s quite wordy too. It could also be done with LINQ, but again, I don’t think UnityScript allows it.

I would have created a class to have easy access to different setups of fields, then just loop through those fields.

class ColorFieldsWindow extends EditorWindow {

	static var colorFields : ColorFields[];

	@MenuItem ("Window/Color Fields")
    static function ShowWindow () {     
        EditorWindow.GetWindow (ColorFieldsWindow);
        
        //This is just for this script, instead use a custom inspector on a GameObject where you can serialize the values
		colorFields = new ColorFields[3];
		for (var i = 0; i<colorFields.Length; i++)
			colorFields *= new ColorFields();*
  •  colorFields[0].name = "Night Sky";*
    
  •  colorFields[1].name = "Night Sun";*
    
  •  colorFields[2].name = "Night Light";*
    

}

  • function OnGUI () {*

  •  DrawColorFields([colorFields[0],colorFields[1],colorFields[2]]);*
    
  • }*

  • function DrawColorFields (passedColorFieldsArray : ColorFields) {*

  •  for (var thisField in passedColorFieldsArray) {*
    
  •  	thisField.color = EditorGUILayout.ColorField(thisField.name, thisField.color);*
    
  •  }*
    
  • }*

  • class ColorFields {*

  •  var name : String;*
    
  •  var color : Color;*
    
  • }*
    }
    If I understand you correctly, you want to have easy and quick access to writing different combinations of your fields, the solution requires you to address them with an int though. In fluid code it finally becomes this line for you to repeat in different orders,
    DrawColorFields([colorFields[int],colorFields[int],colorFields[int]]);
    Or even more simplistic,

    function OnGUI () {

  •  DrawColorFields([0,1,2]);*
    
  • }*

  • function DrawColorFields (colorFieldsPointer : int) {*

  •  for (var i = 0; i<colorFieldsPointer.Length; i++)*
    

colorFields[colorFieldsPointer].color = EditorGUILayout.ColorField(colorFields[colorFieldsPointer_].name, colorFields[colorFieldsPointer*].color);
}
…*

Is this what you were striving for or are there something more to the task that got overlooked?_