Akash Agarwal
Akash Agarwal

Reputation: 2520

Unity compare if two input fields have same value

How can I achieve this? A function can take only 1 argument in Unity for an event. There are two different input fields in the same canvas which I'd like to compare. I'm assuming this can be done using object as argument but I don't know which object I can pass. I tried dragging the canvas and input field gameObject(I'm guessing that's what it's called) to the object parameter field of my selected function but they don't get accepted.

This is where I want to attach the script:-
enter image description here

Upvotes: 1

Views: 1733

Answers (1)

Cenkisabi
Cenkisabi

Reputation: 1076

InputFields are not GameObject. They are objects of UI.InputField class. So in your script import UI with

#using UnityEngine.UI;

Then create 2 InputField object.

public InputField inp1;
public InputField inp2;

then drag your InputFields to them. then you can compare their text like that:

if( inp1.text == inp2.text) {}

Edit: You shouldnt attach a script to there, because the variables may change dynamically. attach this script I have wroten to an empty game object, add new snippet on below to script, and then attach this empty game object to event field that you showed. Then you should select the function will be called when edit ended. For this, create two different function in script for each input field as:

//Call this on EndEdit for inputField1
public void InpField1EndEdit()
{
    //compare input fields here or make what you want
}

//Call this on EndEdit for inputField2
public void InpField2EndEdit()
{
    //compare input fields here or make what you want
} 

Upvotes: 1

Related Questions