-4

I have problem converting type String to UI Input in Unity3D. I want to retrieve the information from database and place them in a textbox (UI Input)

This is the error message. (1st error)

"Cannot convert method group ToString' to non-delegate typeUIInput'. Consider using parentheses to invoke the method"

password = GameObject.Find ("tb_password").GetComponent <UIInput> ().ToString;
Bart
  • 19,692
  • 7
  • 68
  • 77
Sarah
  • 11
  • 2
  • 7
  • Right now, I am confused on how to convert type string to UI Input. My error shows "cannot implicitly convert 'type' string to 'UIInput'. – Sarah May 05 '14 at 06:08
  • 1
    You need to show us your code where you have the error so that we can help you. – SF Lee May 05 '14 at 07:16
  • My codes are as follow. 'username = DB.getName(LoginSystem.userNameStatic); password = DB.getPassword ("password");' – Sarah May 05 '14 at 07:50
  • I am sorry, I am new in asking questions here, if I am doing it wrong, please tell me so. Thanks alot, really need your help for my ques :) – Sarah May 05 '14 at 07:51
  • Please update your question above and put in all relevant code there. Don't put it in the comments. – SF Lee May 05 '14 at 07:59
  • Also, what are the types of `username` and `password`? Are they strings or textboxes? – SF Lee May 05 '14 at 08:01
  • I just changed my question and added the relevant codes. – Sarah May 05 '14 at 08:16
  • Does your `GetComponent()` method returns a `string` or a `UIInput`? – SF Lee May 05 '14 at 08:20
  • It returns a UI Input. – Sarah May 05 '14 at 08:37
  • Then obviously you can't assign a `UIInput` to a `string` variable. If `UIInput` is a textbox, you can get its `Text` property to assign to the variable instead: `username = ...GetComponent().Text;` – SF Lee May 05 '14 at 08:41
  • By the way, what type does `getName()` and `getPassword()` return? – SF Lee May 05 '14 at 08:44
  • After changing it to text, the error shows that Type `UIInput' does not contain a definition for `Text' and no extension method `Text' of type `UIInput' could be found (are you missing a using directive or an assembly reference?)' – Sarah May 05 '14 at 08:47
  • That means your `UIInput` is not a `TextBox`. It's probably a user control or a custom control. Try to find a property or method in the `UIInput` class that returns what you need (eg. username as a `string`). – SF Lee May 05 '14 at 08:57
  • getName() and getPassword() returns the username and password fields! – Sarah May 05 '14 at 09:08
  • No, I mean what _type_ are they? For example, Are they `string`? – SF Lee May 05 '14 at 09:10
  • Actually, can you post the whole error message(s) above? Also include which line(s) the errors are pointing to. That should help us a lot in diagnosing where the error comes from. – SF Lee May 05 '14 at 09:25
  • Show us please. Show us the code where you state that they are strings. The errors you get seem to be suggesting they are not. – Bart May 05 '14 at 09:36
  • Posted the 1st error above. Thanks! – Sarah May 05 '14 at 09:38
  • I posted the 1st error and the line of codes that has the error and the error msg. – Sarah May 05 '14 at 09:38
  • Oh geez. You're simply not calling `ToString()` correctly there. You're missing the `()`, which the error tells you. *"Consider using parentheses to invoke the method"* Then again, that whole line is rather nonsensical and will more than likely not do what you think it does. – Bart May 05 '14 at 09:40
  • Sorry I forgot to paste the brackets as well! – Sarah May 05 '14 at 09:43
  • And now, all my errors are back to "Cannot implicitly convert type 'string' to 'UIInput'. – Sarah May 05 '14 at 09:44
  • Then your `password` is not of the string type. It's a `UIInput`, as we have said before. – Bart May 05 '14 at 09:44
  • How do you think I can solve my errors? – Sarah May 05 '14 at 09:45
  • By leaving out the `ToString()` call. And then you can get your text from the UIInput by doing something like `password.text;`. See also the documentation: http://www.tasharen.com/ngui/docs/class_u_i_input.html – Bart May 05 '14 at 09:48
  • My errors are cleared. I am still unsure on the password.text, could you explain it to me? Thanks alot – Sarah May 05 '14 at 09:52

2 Answers2

1

You are trying to override the label with a string value, which results in type mismatch and actually is not what you've intended. You have to access UIInput property value or defaultValue if you wan't to provide defaults for user - anyway according to your code you probably don't want it.

Anyway, if you are using the latest NGUI, you should correct your code like this:

UIInput username = GameObject.Find ("tb_username").GetComponent <UIInput> ();
UIInput password = GameObject.Find ("tb_password").GetComponent <UIInput> ();

if( username != null )
    username.value = DB.getName(LoginSystem.userNameStatic);
if( password != null )
    password.value = DB.getPassword ("password");

That should work if you correctly find objects with these names you specify, otherwise it won't do anything.

kreys
  • 987
  • 4
  • 21
  • Thanks alot! Still working on it. I just changed my question, hope it helps better! – Sarah May 05 '14 at 08:15
  • Updated my answer according to your question. – kreys May 05 '14 at 08:30
  • Hi, I just tried your codes out and my information did not appear in the UI Input aka textboxes. Any information (codes) you need from me to derive more answers? Thanks alot – Sarah May 05 '14 at 08:40
  • I have specified the objects with the correct names as well. But it has no error. – Sarah May 05 '14 at 08:41
  • Have you attached a `UILabel` to `UIInput` `label` property? If you did you can also try to force the update, like `username.UpdateLabel()`. – kreys May 05 '14 at 08:45
  • According to the question, `username` and `password` are strings. But in your answer, they are something else? – SF Lee May 05 '14 at 09:13
  • @SF Lee: No, according to the question and to the error message (`Cannot implicitly convert type 'string' to 'UIInput'`) `username` and `passwords` are `UIInput` type, and the error is probably on last two lines. If they were strings, the error message would be the other way round: `Cannot implicitly convert type 'UIInput' to 'string'. Anyway, the types obviously should be specified in the question and the're not, which leads to misunderstanding. – kreys May 05 '14 at 09:17
  • @SFLee if you get your errors on the last two lines of your code, then username is by no means a string. `username = GameObject.Find ("tb_username").GetComponent ();` tells us it isn't. – Bart May 05 '14 at 09:23
  • Well, the question did say: _The username and password are both type String._ Maybe the error comes from `GameObject.Find(...)` where the it's expecting a `UIInput` parameter? – SF Lee May 05 '14 at 09:23
  • But if they would be `string` wouldn't the error message from the title be the other way round? Maybe the asker should clarify that things once again. – kreys May 05 '14 at 09:25
  • I agree. The whole code looks like there are more than 1 errors to me. – SF Lee May 05 '14 at 09:26
0

You could access the UILabel component under the NGUI's input widget instead of the UIInput component at the root of the input widget and set the text in the label (UILabel.text = "my text"). You could also make a function that updates the label text and pass the text as a parameter for this function. Call the function when you want to update the text.