Hello, I have made a script, but it gives the error: A field innitialiser cannot reference the nonstatic field, method, or property 'WoordRaadInvoer' The scene has a UI text object and a UI InputField. The script needs to work like this: randomly generate a word to display in the UI text object. If the text, written by the user is equal to a certain word, do something (like print "done") and start over with a new set. Beneith the code I have some code that was working at first, but stopped working after I added the code for the UI text. Code (CSharp): using UnityEngine; using UnityEngine.UI; using System.Collections; public class WoordRaad : MonoBehaviour { public Text deTekst; //Selecteer de tekst die vertaald moet worden. Drop het tekstobject hierin. public GameObject laadInvoer; InputField input = laadInvoer.GetComponent<InputField>(); //public GameObject tekstObject; //load the text object containing the word you need to translate //= GameObject.FindWithTag("invoer") void Start() { StartSpel (); } void Update () { } void StartSpel(){ int willekeurigGetal; willekeurigGetal = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal woorden if (willekeurigGetal == 1) {Woord1();} if (willekeurigGetal == 2) {Woord2();} } void Woord1(){ deTekst.text = "broek"; //verander de tekst in het tekstobject. //var input = GameObject.FindWithTag("invoer");//make a variable equal to the user input if (input == "spodnie" || input == "Spodnie") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt print ("gelukt"); } } void Woord2(){ deTekst.text = "muis"; //verander de tekst in het tekstobject. //var input = GameObject.FindWithTag("invoer"); //make a variable equal to the user input if (input == "mysza" || input == "Mysza") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt print ("gelukt"); } } // void Update () // { // string woordInEigenTaal; //make a string // woordInEigenTaal = tekstObject.GetComponent<Text>().text as string; //set the string to the text object text // // if (woordInEigenTaal == "test") {//if the word in the text object is equal to test, load the function WoordVergelijk // WoordVergelijk (); // } // } // void WoordVergelijk() // { // // var input = gameObject.GetComponent<InputField> (); //make a variable equal to the user input // // if (input.text == "bruin") { //if the text part of the user input is equal to bruin, print gelukt // print ("gelukt"); // } // // } }
Is this the script the error is originating from? 'WoordRaadInvoer' doesn't make an appearance in this script.
It is the script. It is referencing to line 10, 28. Must be the name of the script, following laadInvoer. So together WoordLaad.laadInvoer
You cannot declare a variable that way outside a function. Code (CSharp): InputField input; void Start() { //Move it to the Start function for example. input = laadInvoer.GetComponent<InputField>(); StartSpel (); }
Thanks Mich_9. The code works like this. Though it does not do anything with the tekst I write in the InputField... Code (CSharp): using UnityEngine; using UnityEngine.UI; using System.Collections; public class WoordRaad : MonoBehaviour { public Text deTekst; //Selecteer de tekst die vertaald moet worden. Drop het tekstobject hierin. public GameObject laadInvoer; InputField input; //InputField input = laadInvoer.GetComponent<InputField>(); //public GameObject tekstObject; //load the text object containing the word you need to translate //= GameObject.FindWithTag("invoer") void Start() { input = laadInvoer.GetComponent<InputField>(); StartSpel (); } void Update () { } void StartSpel(){ int willekeurigGetal; willekeurigGetal = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal woorden if (willekeurigGetal == 1) {Woord1();} if (willekeurigGetal == 2) {Woord2();} } void Woord1(){ deTekst.text = "broek"; //verander de tekst in het tekstobject. //var input = GameObject.FindWithTag("invoer");//make a variable equal to the user input if (input.text == "spodnie" || input.text == "Spodnie") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt print ("gelukt"); } } void Woord2(){ deTekst.text = "muis"; //verander de tekst in het tekstobject. //var input = GameObject.FindWithTag("invoer"); //make a variable equal to the user input if (input.text == "mysza" || input.text == "Mysza") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt print ("gelukt"); } } // void Update () // { // string woordInEigenTaal; //make a string // woordInEigenTaal = tekstObject.GetComponent<Text>().text as string; //set the string to the text object text // // if (woordInEigenTaal == "test") {//if the word in the text object is equal to test, load the function WoordVergelijk // WoordVergelijk (); // } // } // void WoordVergelijk() // { // // var input = gameObject.GetComponent<InputField> (); //make a variable equal to the user input // // if (input.text == "bruin") { //if the text part of the user input is equal to bruin, print gelukt // print ("gelukt"); // } // // } }
I tried to use the update function, but still nothing appears: Code (CSharp): using UnityEngine; using UnityEngine.UI; using System.Collections; public class WoordRaad : MonoBehaviour { public Text deTekst; //Selecteer de tekst die vertaald moet worden. Drop het tekstobject hierin. public GameObject laadInvoer; InputField input; public bool woord; //InputField input = laadInvoer.GetComponent<InputField>(); //public GameObject tekstObject; //load the text object containing the word you need to translate //= GameObject.FindWithTag("invoer") void Start() { input = laadInvoer.GetComponent<InputField>(); StartSpel (); } void Update () { if (woord == true) { Debug.Log("gelukt"); } } void StartSpel(){ int willekeurigGetal; willekeurigGetal = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal woorden if (willekeurigGetal == 1) {Woord1();} if (willekeurigGetal == 2) {Woord2();} } void Woord1(){ deTekst.text = "broek"; //verander de tekst in het tekstobject. //var input = GameObject.FindWithTag("invoer");//make a variable equal to the user input if (input.text == "spodnie" || input.text == "Spodnie") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt woord=true; } } void Woord2(){ deTekst.text = "muis"; //verander de tekst in het tekstobject. //var input = GameObject.FindWithTag("invoer"); //make a variable equal to the user input if (input.text == "mysza" || input.text == "Mysza") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt woord=true; } } }
there is a basic problem. as soon as you start your game the input is read and tested immediately. the player has no time to actually input anything. second problem in start spel what if the number picked is 3? there is a fall through there where if 3 is picked it will output nothing and nothing will happen. startspel should be triggered by a ui event.