Search Unity

InputField removing itself when i click play

Discussion in '2D' started by Mucahit53, Jan 6, 2021.

  1. Mucahit53

    Mucahit53

    Joined:
    Dec 15, 2020
    Posts:
    10
    i assigned my inputfield on inspector but when i start game its removing itself and i have to put it manually on game.

    and i use this code but its didnt working.

    Code (CSharp):
    1. inputMiktar = GameObject.Find("InputFieldFiyatGir").GetComponent<InputField>();
    how i solve this problem help me
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Mucahit53

    This is a general scripting question - so why are you asking this in 2D forum?

    "InputField removing itself when i click play"

    You are trying to find your GameObject, and you actually don't find it, and you end up assigning a null value to your variable. You didn't either say, if you actually get some errors in your console.

    Without complete code it is hard to say what happens. Maybe edit your post and add full code here.

    But in general, make sure you have a GameObject named exactly "InputFieldFiyatGir" and make sure it really does have a InputField component. It is usually best to split your code a bit if you are unsure if something exists. That way you can get proper easy to read errors when you run your code:
    Code (CSharp):
    1. public class Testing : MonoBehaviour
    2. {
    3.     [SerializeField] private GameObject obj;
    4.     [SerializeField] private BoxCollider col;
    5.  
    6.     void Start()
    7.     {
    8.         obj = GameObject.Find("Cube");
    9.         Debug.Log("obj:" + obj.name);
    10.  
    11.         col = obj.GetComponent<BoxCollider>();
    12.         Debug.Log("col:" + col.ToString());
    13.     }
    14. }
    If I were you, I would read some threads in general scripting. You can find this kind of questions on daily basis there, and you'll find several good answers too!
     
  3. Mucahit53

    Mucahit53

    Joined:
    Dec 15, 2020
    Posts:
    10
    sorry im new on forum

    Ekran Alıntısı.PNG

    Code (CSharp):
    1. private GameObject obj;
    2.     private InputField inputMiktar;
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.        
    7.             obj = GameObject.Find("InputFieldFiyatGir");
    8.             Debug.Log("obj:" + obj.name);
    9.  
    10.             inputMiktar = obj.GetComponent<InputField>();
    11.             Debug.Log("Input Miktar:" + inputMiktar.ToString());
    Log with 0 is unimportant another thing

    79. line :

    Code (CSharp):
    1. verdiginFiyatGosterge.text = "$ " + inputMiktar.text;
    your code isnt assign
     
  4. Mucahit53

    Mucahit53

    Joined:
    Dec 15, 2020
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.UI;
    6.  
    7. public class AnaScript : MonoBehaviour
    8. {
    9.     public int Para;
    10.     public GameObject satıcıPanel;
    11.     public GameObject satıcı1;
    12.     public GameObject buton1;
    13.     public GameObject buton2;
    14.     public GameObject buton3;
    15.     public GameObject buton4;
    16.     public TextMeshProUGUI paraGosterge;
    17.     bool ifdongusu;
    18.     public TextMeshProUGUI maksHız;
    19.     public TextMeshProUGUI arabaModeli;
    20.     public TextMeshProUGUI arabaMarkasi;
    21.     public TextMeshProUGUI nadirlik;
    22.     private int randomRange1;
    23.     public TextMeshProUGUI kendiniTanıtma;
    24.     public GameObject fiyatpanel;
    25.     public TextMeshProUGUI verdiginFiyatGosterge;
    26.     public TextMeshProUGUI tahminiFiyat;
    27.     int girilenFiyat;
    28.  
    29.  
    30.  
    31.     public GameObject obj;
    32.     public InputField inputMiktar;
    33.     // Start is called before the first frame update
    34.     void Start()
    35.     {
    36.        
    37.             obj = GameObject.Find("InputFieldFiyatGir");
    38.             Debug.Log("obj:" + obj.name);
    39.  
    40.             inputMiktar = obj.GetComponent<InputField>();
    41.         Debug.Log("Input Miktar:" + inputMiktar.ToString());
    42.  
    43.         fiyatpanel.SetActive(false);
    44.         buton4.SetActive(true);
    45.         satıcıPanel.SetActive(false);
    46.         satıcı1.SetActive(false);
    47.         Para = 35000;
    48.         kendiniTanıtma.text = " ";
    49.         inputMiktar = gameObject.GetComponent<InputField>();
    50.        
    51.     }
    52.  
    53.     public void satıcıvoid()
    54.     {
    55.         satıcı1.SetActive(true);
    56.         ifdongusu = true; // RASTGELEDE SATICI1SETACTIVENIN ICINE ATILICAK
    57.     }
    58.  
    59.     public void ilgimiCekmedi()
    60.     {
    61.         StartCoroutine("panelkapat", 2.0f);
    62.     }
    63.  
    64.     public void bakalim()
    65.     {
    66.         StartCoroutine("bakalimbutonlar", 2.0f);
    67.         randomRange1 = Random.Range(1, 2);
    68.         Debug.Log("Rastgele Sayı: " + randomRange1);
    69.     }
    70.  
    71.    
    72.  
    73.     // Update is called once per frame
    74.     void Update()
    75.     {
    76.         Debug.Log(girilenFiyat);
    77.    
    78.  
    79.         verdiginFiyatGosterge.text = "$ " + inputMiktar.text;
    80.         //girilenFiyat = int.Parse(inputMiktar.text);
    81.         inputMiktar.text = girilenFiyat.ToString();
    82.  
    83.  
    84.         paraGosterge.text = Para.ToString();
    85.         if (Para > 9999999)
    86.         {
    87.             Para = 9999999;
    88.         }
    89.         if (ifdongusu == true)
    90.         {
    91.          
    92.             if (randomRange1 == 1)
    93.             {
    94.                 kendiniTanıtma.text = "Hadi, Ilk senin fiyatini duyalim.";
    95.                 fiyatpanel.SetActive(true);
    96.                 tahminiFiyat.text = "- Tahmini Fiyat: $ 168000";
    97.             }
    98.             if (randomRange1 == 2)
    99.             {
    100.                 kendiniTanıtma.text = "Hadi, Ilk senin fiyatini duyalim.";
    101.                 fiyatpanel.SetActive(true);
    102.                 tahminiFiyat.text = "- Tahmini Fiyat: $ 168000";
    103.             }
    104.  
    105.  
    106.             kendiniTanıtma.text = "Selam, benim adim Arda ve eski arabami satmak istiyorum senin alabilecegini duydum...";
    107.             buton4.SetActive(false);
    108.             satıcıPanel.SetActive(true);
    109.             nadirlik.text = "Nadirlik: Cok Nadir";
    110.             nadirlik.color = Color.green;
    111.             arabaMarkasi.text = "Araba Markasi: Tuyuta";
    112.             arabaModeli.text = "Araba Modeli: Subra 1997";
    113.             maksHız.text = "Maksimum Hiz: 295 KmH";
    114.         }
    115.         else
    116.         {
    117.             buton4.SetActive(true);
    118.             nadirlik.text = "Nadirlik: ";
    119.             nadirlik.color = Color.white;
    120.             arabaMarkasi.text = "Araba Markasi: ";
    121.             arabaModeli.text = "Araba Modeli: ";
    122.             maksHız.text = "Maksimum Hiz: ";
    123.         }
    124.        
    125.        
    126.     }
    127.     IEnumerator panelkapat()
    128.     {
    129.         yield return new WaitForSeconds(0.2f);
    130.         satıcı1.SetActive(false);
    131.         ifdongusu = false;
    132.     }
    133.  
    134.     IEnumerator bakalimbutonlar()
    135.     {
    136.         yield return new WaitForSeconds(0.3f);
    137.         buton1.SetActive(false);
    138.         buton2.SetActive(false);
    139.         buton3.SetActive(false);
    140.         buton4.SetActive(false);
    141.     }
    142. }
    143.  
    full code, its like dealer's life game
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    I'm not sure about what causes that error in line 79. Didn't read the full code.

    But strings are objects, and objects can be null. So you should check your string before using it. Otherwise you might get a null reference exception, because your variable might be null:
    Code (CSharp):
    1. // a string with null value
    2. string myText = null;
    3.  
    4. // check before using
    5. if (string.IsNullOrEmpty(myText) == false)
    6. {
    7.     // string wasn't empty, print string
    8.     Debug.Log("MyText:" + myText.ToString());
    9. }
    10. else
    11. {
    12.     // string was empty, do something else
    13.     Debug.Log("MyText: -");
    14. }
    Note. If you are ever considering sharing your code or asking questions related to your code, always use english language for variables. It is not a native language for many of us, but using english makes it more likely you'll get answers.
     
  6. Mucahit53

    Mucahit53

    Joined:
    Dec 15, 2020
    Posts:
    10
    thanks try other object and inputfield its worked now i am rebuilding the trouble inputfield