Unity 5.3 dropdown menu - drop-down-menu

I am trying to set the text of my dropdown menu options by scripting but I'm geting this error
NullReferenceException: Object reference not set to an instance of an object
My code:
function Start () {
var dropdown = GameObject.Find("Dropdown");
dropdown.options.Clear();
dropdown.options.Add(new dropdown.OptionData("tile1"));
}
This is my first project in Unity.

It's possible that Dropdown does not exist yet in the scene when Start() is called. Perhaps consider adding a public field instead of using GameObject.Find():
public GameObject dropdown;
void Start()
{
//... your code here
}
Then in the scene, drag your "Dropdown" object onto the object this script is on.

Related

Xamarin.Forms UWP - InvalidCastException when trying to set TextDecorations for TextBlock

In a Xamarin.Forms project, I'm trying to allow underlined Labels. So I have a custom renderer, and I'm trying to do something simple:
Control.TextDecorations = TextDecorations.Underline;
It compiles just fine, but when the app launches, I'm getting an InvalidCastException on that line which says:
System.InvalidCastException: 'Unable to cast object of type 'Windows.UI.Xaml.Controls.TextBlock' to type 'Windows.UI.Xaml.Controls.ITextBlock5'.'
Here's a screenshot of the exception:
Also, when inspecting the Control, I noticed there are a ton of InvalidCastException exceptions on other properties of the TextBlock control as well - here's a small sample:
Why is it trying to cast to a ITextBlock5 type? Is this a UWP bug? Is there a workaround to get underline to work?
The TextDecorations property is documented on MSDN to be supported on version 15063 and later.
You can use the ApiInformation class to check at runtime whether the property is avaiable or not.
According to the Microsoft documentation, the TextDecorations property wasn't introduced until version 15063. You're probably getting that exception because you're on an earlier version of Windows.
As a workaround, you can create an Underline() object and add a Run() object to the Underline object's Inlines collection, like this:
// first clear control content
Control.Inlines.Clear();
// next create new Underline object and
// add a new Run to its Inline collection
var underlinedText = new Underline();
underlinedText.Inlines.Add(new Run { Text = "text of xamarin element here" });
// finally add the new Underline object
// to the Control's Inline collection
Control.Inlines.Add(underlinedText);
Inside the custom renderer's OnElementChanged() override method, it would look something like this:
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
var view = e.NewElement as LabelExt; // LabelExt => name of PCL custom Label class
var elementExists = (view != null && Control != null);
if (!elementExists)
{
return;
}
// add underline to label
Control.Inlines.Clear();
var underlinedText = new Underline();
underlinedText.Inlines.Add(new Run { Text = view.Text });
Control.Inlines.Add(underlinedText);
}

How do I add a controller (MVC) to my JavaFX application which I wrote in Java?

So I created an app containing the following classes: Main.java (where launch is along with window, scenes, buttons, etc.), Students.java (students), Connection.java (JDBC connects to MySQL database). There's also a class for login (with its own window, scenes, buttons), a class for charts (opens up a Pie Chart) as well as a confirmbox and alertbox (again with windows, scenes and buttons)...
I then read that good practice is to use MVC by looking through different tutorials plus previous answers here. Additionally the best tutorial was this: http://code.makery.ch/library/javafx-8-tutorial/part1/
The problem with this tutorial however was that it uses scene builder... I wish to create my application by writing code instead. And with my existing code (I can run the app on my IDE and buttons etc. work perfectly, fetches data from database too...) but what do I do with connector?
Do I create a new one?
(NOTE: all my classes are in .src)
A javafx controller is used to control UI elements. One way to attach controller is by adding fx:controller="controllerClassLocation" in your fxml file.
Another way would be to add controller in your code but this also involves fxml file as shown below.
String fxmlPath="MainLayout.fxml";
private Scene getScene(String fxmlPath, ControllerClass controller) {
FXMLLoader loader;
Parent parent;
Scene scene;
try {
//not FXMLLoader.load(getClass().getResource(fxmlPath)
loader = new FXMLLoader(getClass().getResource(fxmlPath));
loader.setController(controller);
parent = loader.load();
} catch (Exception e) {
e.printStackTrace();
return null;
}
scene = new Scene(parent);
return scene;
}
You use a controller if you want to bind your javafx UI elements.
If you do not want to use fxml file then you will have to code all the UI elements and bind to their respective functions and events for controlling them. For eg: In the below example a button btn is created and an event is handled when the button is clicked. The Button btn is binded to the function setOnAction(new EventHandler()) .
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 500, 250);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
You can create a controller class just like you would using FXML. Create an instance of the controller and pass on all references of your controls that you created in the code where you create all the controls.
Doing this way, you cannot leverage the benefits of FXML binding, such as fx:id, fx:controller and event bindings.

Crash for second view object's name assign

I am new to Xamarin and MVVMCross. So I have created 2 views. Login and Register. I have a button on Login to goto Register view and I am going there by this code in the Login's ViewModel:
// method when user tap register button
public IMvxCommand NavigateRegister
{
get { return new MvxCommand(() => ShowViewModel<RegisterViewModel>()); }
}
It works ok the Register Page opens well. But once I assign Name for a single object on Register view (a textEdit), the app crash when I tap on the Register button.
Below is the error msg:
Xamarin.iOS: Received unhandled ObjectiveC exception:
NSUnknownKeyException [
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key regNameEdit.
EDIT:
More details: I already assigned the name (see pic below), but still crash:
And the view also been assigned to its Class "CreateAccount". But I am noticing the class declaration has "partial" darkened out in the "public partial class CreateAccount : MvxViewController" line. That's the only noticeable difference btw this class and the first one.
using MvvmCross.Binding.BindingContext;
using MvvmCross.iOS.Views;
namespace MyApp.iOS.Views
{
public partial class CreateAccount : MvxViewController
{
public CreateAccount() : base("CreateAccount", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
Title = "Register";
var set = this.CreateBindingSet<CreateAccount, Core.ViewModels.CreateAccountModel>();
set.Bind(regNameEdit).To(vm => vm.NameStr);
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
}
}
The Bind(regNameEdit) also is an error (not detecting the textedit still)
This usually means that the a control is not defined in the View/ViewController class in your case the regNameEdit.
Make sure you created the back Property for this Edit and that the class assigned to the XIB is the one containing this property.
If you are using Xamarin Studio Designer you create the back property selecting the UIControl in the XIB/StoryBoard and setting a name, then enter.
This will create a property with the name you specified accessible in the ViewController.
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.MyUITextField.Text = string.Empty;
}
UPDATE
When using Storyboard:
Try this: Remove the name from the TextField and also remove the name from the ViewController class, then clean your project and rebuild. Re-add the class to the ViewController but when doing it click over the yellow icon in the bottom, there put the name and hit enter. Continue with the TextField, select it put the name and hit enter.
UPDATE # 2
When using XIBs
When you create the ViewController from the menu, Xamarin Studio will create both the ViewController class and the XIB file and will associate one with the other so here you don't have to do anything else to link them.
For the TextField you will need to do it adding the name as previously indicated.
Try this: Remove the name of the UITextField and save and clean/rebuild the project then add the name and hit enter.
Something you can do to verify if there's any problem, double click on the button in the XIB and this should take you to the ViewController class to create a method.
Hope this helps.

How to change content of unity text?

I create an application on unity3d. I use Unity 5.0.2f1 (64-bit). I create a new project and add an empty gameobject. I click gameobject and add a gui text from inspector. My gui text is below :
I want to change the contenf of this text at each frame. I use below code but it doesn't change it.
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
private int count = 0;
public GUIText guiText;
// Use this for initialization
void Start () {
guiText.text = "Counter";
}
}
What should I do? I see asddffbhfbehfbhj every time.
Attach this script to your gameobject(the same game object that guitext is attached to)then assign the guitext component of your game object to your script via inspector(drag you guitext component and drop it in guitext variable that you see within your script component in inspector)

JavaFX: Focusing textfield programmatically

I wrote an application with JavaFX which will only be usable with keyboard's arrows.
So I prevented MouseEvent on Scene's stage, and I "listen" to KeyEvents.
I also switched off focusability of all nodes :
for(Node n : children) {
n.setFocusTraversable(false);
Now I have some textfields, checkboxes, and buttons.
I would like to change the state of my input controls (textfield, checkbox,..) programatically: for example I would like to enter the textfield to edit the content programatically.
So my question is: how to enter in a non-focus-traversable textfield?
Because textfield.requestFocus(); doesn't work anymore since I set false to focustraversable property of my textfield.
Thanks
By
n.setFocusTraversable(false);
the node is made non-focus-traversable instead of non-focusable. It can still be focused for example by mouse or programmatically. Since you prevented mouse events, here the other option:
Platform.runLater(new Runnable() {
#Override
public void run() {
textfield.requestFocus();
}
});
Scene scene = new Scene(root);
EDIT: as per comment,
The javadoc of requestFocus states:
... To be eligible to receive the focus, the node must be part of a scene,
it and all of its ancestors must be visible, and it must not be
disabled. ...
So this method should be called after construction of scene graph as follow:
Scene scene = new Scene(root);
textfield.requestFocus();
However, Platform.runLater in the above will run at the end, after the main method start(), which ensures the call of requestFocus will be after scene graph cosntruction.
There maybe other reasons depending on the requestFocus implementation code.
set the .requestFocus(); on initialize method to fire on .fxml file loading controller
#Override
public void initialize(URL url, ResourceBundle rb) {
/* the field defined on .fxml document
#FXML
private TextField txtYear;
*/
txtYear.requestFocus();
}

Resources