- Home /
Get string from InputField
I dont know how to get string from Inputfield. I need string to post name to my scoreboard. I tried the following code, but it gives me error: "A field initializer cannot reference the nonstatic field, method, or property `Post_score.saveField'". Im using Unity 4.6.3
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Post_score : MonoBehaviour
{
public string addScoreURL = "http://mywebpage.com/scores/addscore.php?";
public InputField saveField;
string name = saveField.text;
int tocke = 10;
public void poslji()
{
StartCoroutine(PostScores(name, tocke));
}
IEnumerator PostScores(string name, int tocke)
{
string post_url = addScoreURL + "name=" + name + "&score=" + tocke;
WWW hs_post = new WWW(post_url);
yield return hs_post;
if (hs_post.error != null)
{
//err.text = "There was an error posting the high score: " + hs_post.error;
}
else
{
Application.LoadLevel("Menu");
}
}
}
Answer by TeohRIK · Apr 28, 2015 at 12:46 AM
StartCoroutine(PostScores(saveField.text, tocke));
I think this should work, because your problem is your name variable only get the data from the save field during then program initialization only, then if the player make the change, the name variable won't get updated. Solution either be update your name variable for every loop or just get thr data when you need just like the code I provide
Your answer
Follow this Question
Related Questions
Public text to multi line 1 Answer
Assigning Triangles from String Array issue 0 Answers
Selecting a part of string in input field 1 Answer
How to get an input and put it into a scriptable object? 1 Answer
Inputfield set as Variable 1 Answer