Writing dropdown selections to file
Hi,
I'm fairly new to Unity and I have a dropdown menu on my UI menu and I'm attempting to write the currently selected option from the dropdown menu to a .txt file on pressing the return key but for some reason I keep getting the error:
Error CS1061: Type Dropdown' does not contain a definition for
value' and no extension method value' of type
Dropdown' could be found. Are you missing an assembly reference? (CS1061) (Assembly-CSharp)
Ive been experimenting with it first by simply trying to change some text to whatever option has been selected in the dropdown menu but I can't get past this error.
Heres the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Dropdown : MonoBehaviour
{
public Dropdown menu;
public Text text;
void Start()
{
}
public void ChangeOption()
{
switch (menu.value)
{
case 0:
graph.text = "Deadlift";
break;
case 1:
graph.text = "Bench Press";
break;
case 2:
graph.text = "Overhead Press";
break;
case 3:
graph.text = "Squat";
break;
}
}
}
Any help would be greatly appreciated!
Thanks!
Answer by GoeTeeks · Feb 17, 2016 at 08:55 PM
Hi,
I'm trying to figure out how to print the text of the dropdown into a UI text. But let me point out a couple of things I noticed. You said you want to write to a text file? The syntax for that is quite a bit different than what you're using. The Text object you created is for Unity's UI Text. To write to a text file, you need to do a few things first. You might want to look up "how to write to a text file" before moving too far forward (I can't remember the exact syntax as it's been a while since I've used it. You need the right using, then you need to use something like System.IO.WriteToLine (/path/file.txt) or something like that). There's a few options.
If you're trying to write to the UI Text instead, just know that it's not considered a .txt file. I hope this helps a little bit.
Alright, as to how to print the string within a dropdown, use this:
menu.options[indexOfValue].text
where "indexOfValue" is the int value of the Dropdown (0 is the first option, 1 is the second, and so on).