Search Unity

help with script

Discussion in 'Scripting' started by danicrem, Feb 9, 2016.

  1. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    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):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5.  
    6. public class WoordRaad : MonoBehaviour {
    7.  
    8.     public Text deTekst; //Selecteer de tekst die vertaald moet worden. Drop het tekstobject hierin.
    9.     public GameObject laadInvoer;
    10.     InputField input = laadInvoer.GetComponent<InputField>();
    11.  
    12.  
    13.     //public GameObject tekstObject; //load the text object containing the word you need to translate
    14.     //= GameObject.FindWithTag("invoer")
    15.  
    16.     void Start()
    17.     {
    18.         StartSpel ();
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.  
    24.    
    25.     }
    26.  
    27.     void StartSpel(){
    28.         int willekeurigGetal;
    29.         willekeurigGetal = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal woorden
    30.         if (willekeurigGetal == 1) {Woord1();}
    31.         if (willekeurigGetal == 2) {Woord2();}
    32.  
    33.  
    34.     }
    35.  
    36.     void Woord1(){
    37.         deTekst.text = "broek"; //verander de tekst in het tekstobject.
    38.         //var input = GameObject.FindWithTag("invoer");//make a variable equal to the user input
    39.  
    40.         if (input == "spodnie" || input == "Spodnie") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt
    41.             print ("gelukt");
    42.             }
    43.         }
    44.  
    45.     void Woord2(){
    46.         deTekst.text = "muis"; //verander de tekst in het tekstobject.
    47.  
    48.         //var input = GameObject.FindWithTag("invoer"); //make a variable equal to the user input
    49.         if (input == "mysza" || input == "Mysza") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt
    50.                 print ("gelukt");
    51.             }
    52.         }
    53.  
    54.  
    55. //    void Update ()
    56. //    {
    57.         //        string woordInEigenTaal; //make a string
    58.         //        woordInEigenTaal = tekstObject.GetComponent<Text>().text as string; //set the string to the text object text
    59.         //
    60.         //        if (woordInEigenTaal == "test") {//if the word in the text object is equal to test, load the function WoordVergelijk
    61.         //            WoordVergelijk ();
    62.         //        }
    63.  
    64. //    }
    65. //    void WoordVergelijk()
    66. //    {
    67. //
    68. //        var input = gameObject.GetComponent<InputField> (); //make a variable equal to the user input
    69. //
    70. //        if (input.text == "bruin") { //if the text part of the user input is equal to bruin, print gelukt
    71. //            print ("gelukt");
    72. //        }
    73. //
    74. //    }
    75.  
    76.  
    77. }
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Is this the script the error is originating from? 'WoordRaadInvoer' doesn't make an appearance in this script.
     
  3. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    It is the script. It is referencing to line 10, 28. Must be the name of the script, following laadInvoer. So together WoordLaad.laadInvoer
     
  4. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    119
    You cannot declare a variable that way outside a function.
    Code (CSharp):
    1.     InputField input;
    2.     void Start()
    3.     {
    4.         //Move it to the Start function for example.
    5.         input = laadInvoer.GetComponent<InputField>();
    6.         StartSpel ();
    7.     }
     
  5. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    Thanks Mich_9. The code works like this. Though it does not do anything with the tekst I write in the InputField...

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5.  
    6. public class WoordRaad : MonoBehaviour {
    7.  
    8.     public Text deTekst; //Selecteer de tekst die vertaald moet worden. Drop het tekstobject hierin.
    9.     public GameObject laadInvoer;
    10.     InputField input;
    11.  
    12.     //InputField input = laadInvoer.GetComponent<InputField>();
    13.  
    14.  
    15.     //public GameObject tekstObject; //load the text object containing the word you need to translate
    16.     //= GameObject.FindWithTag("invoer")
    17.  
    18.     void Start()
    19.     {
    20.        
    21.         input = laadInvoer.GetComponent<InputField>();
    22.         StartSpel ();
    23.     }
    24.  
    25.     void Update ()
    26.     {
    27.  
    28.    
    29.     }
    30.  
    31.     void StartSpel(){
    32.         int willekeurigGetal;
    33.         willekeurigGetal = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal woorden
    34.         if (willekeurigGetal == 1) {Woord1();}
    35.         if (willekeurigGetal == 2) {Woord2();}
    36.  
    37.  
    38.     }
    39.  
    40.     void Woord1(){
    41.         deTekst.text = "broek"; //verander de tekst in het tekstobject.
    42.         //var input = GameObject.FindWithTag("invoer");//make a variable equal to the user input
    43.  
    44.         if (input.text == "spodnie" || input.text == "Spodnie") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt
    45.             print ("gelukt");
    46.             }
    47.         }
    48.  
    49.     void Woord2(){
    50.         deTekst.text = "muis"; //verander de tekst in het tekstobject.
    51.  
    52.         //var input = GameObject.FindWithTag("invoer"); //make a variable equal to the user input
    53.         if (input.text == "mysza" || input.text == "Mysza") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt
    54.                 print ("gelukt");
    55.             }
    56.         }
    57.  
    58.  
    59. //    void Update ()
    60. //    {
    61.         //        string woordInEigenTaal; //make a string
    62.         //        woordInEigenTaal = tekstObject.GetComponent<Text>().text as string; //set the string to the text object text
    63.         //
    64.         //        if (woordInEigenTaal == "test") {//if the word in the text object is equal to test, load the function WoordVergelijk
    65.         //            WoordVergelijk ();
    66.         //        }
    67.  
    68. //    }
    69. //    void WoordVergelijk()
    70. //    {
    71. //
    72. //        var input = gameObject.GetComponent<InputField> (); //make a variable equal to the user input
    73. //
    74. //        if (input.text == "bruin") { //if the text part of the user input is equal to bruin, print gelukt
    75. //            print ("gelukt");
    76. //        }
    77. //
    78. //    }
    79.  
    80.  
    81. }
     
  6. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    I tried to use the update function, but still nothing appears:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5.  
    6. public class WoordRaad : MonoBehaviour {
    7.  
    8.     public Text deTekst; //Selecteer de tekst die vertaald moet worden. Drop het tekstobject hierin.
    9.     public GameObject laadInvoer;
    10.     InputField input;
    11.     public  bool woord;
    12.  
    13.     //InputField input = laadInvoer.GetComponent<InputField>();
    14.  
    15.  
    16.     //public GameObject tekstObject; //load the text object containing the word you need to translate
    17.     //= GameObject.FindWithTag("invoer")
    18.  
    19.     void Start()
    20.     {
    21.         input = laadInvoer.GetComponent<InputField>();
    22.  
    23.         StartSpel ();
    24.     }
    25.  
    26.     void Update ()
    27.     {
    28.         if (woord == true) {
    29.             Debug.Log("gelukt");
    30.         }
    31.    
    32.     }
    33.  
    34.     void StartSpel(){
    35.         int willekeurigGetal;
    36.         willekeurigGetal = Random.Range(1, 3); //range moet 1 groter zijn dan maximaal aantal woorden
    37.         if (willekeurigGetal == 1) {Woord1();}
    38.         if (willekeurigGetal == 2) {Woord2();}
    39.  
    40.  
    41.     }
    42.  
    43.     void Woord1(){
    44.         deTekst.text = "broek"; //verander de tekst in het tekstobject.
    45.         //var input = GameObject.FindWithTag("invoer");//make a variable equal to the user input
    46.  
    47.         if (input.text == "spodnie" || input.text == "Spodnie") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt
    48.         woord=true;
    49.             }
    50.         }
    51.  
    52.     void Woord2(){
    53.         deTekst.text = "muis"; //verander de tekst in het tekstobject.
    54.  
    55.         //var input = GameObject.FindWithTag("invoer"); //make a variable equal to the user input
    56.         if (input.text == "mysza" || input.text == "Mysza") { //if the text part of the user input is equal to spodnie or Spodnia, print gelukt
    57.             woord=true;
    58.             }
    59.         }
    60.  
    61.  
    62. }
    63.  
    64.  
     
  7. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    Anybody knows how to fix the script? laadInvoer is linked to the InputField object.
     
  8. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    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.