R
R
Roman2017-11-02 12:17:20
C++ / C#
Roman, 2017-11-02 12:17:20

Where is the error in the code (input in Unity)?

Hello!
There is a code (yes, clumsy, because I'm a beginner):

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class Chose : MonoBehaviour {
  public int i=0;
  public string [] m = new string[100];
  public List <string> films = new List<string>(100);
  public void Changename(string str)
  {
    films.Add(str);
    i += 1;
    Debug.Log("Имя" + i +", название фильма - " + str);
  }
  public void WriteAllOfThem()
  {
    foreach (string p in films)
    {
      Debug.Log (p);
    }
  }
}
class Film
{
  public string Name { get; set; } 
}

And in theory, in Unity, the user must enter text, then press Enter, so fill everything in and click on the button so that everything is displayed in Debug.Log();
Here is a screenshot from Unity2D:
59fae298eebcf866342494.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2017-11-02
@ananas_roma

You may well have Changename without a parameter.
As you indicated on your screen with red arrows, this is not the ability to transfer text from the text field, but to the method to shove something from the Editor (in no way connected with the input),
you need to specify somewhere in the script or get a link to the
InputField

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Collections.Generic;

public class Chose : MonoBehaviour {
public InputField nameField;
  public int i=0;
  public string [] m = new string[100];
  public List <string> films = new List<string>(100);
  public void Changename()
  {
    string newName = nameField.text;
    films.Add(newName );
    i += 1;
    Debug.Log("Имя" + i +", название фильма - " + newName );
  }
  public void WriteAllOfThem()
  {
    foreach (string p in films)
    {
      Debug.Log (p);
    }
  }
}
class Film
{
  public string Name { get; set; } 
}

only in the inspector don't forget to link to this text field.
and so, by the way, using this link, we can substitute text in this field and change the format, and in general, how did you want to work with it without a link to the element?))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question