- Home /
Why InputField don't have the property text ?
I took the example from the unity documents: Input Field
And i added a empty GameObject to the Hierarchy then i added the component InputField to the Inspector on the empty GameObject.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class InputField : MonoBehaviour
{
private InputField mainInputField;
// Use this for initialization
void Start ()
{
mainInputField = GetComponent<InputField>();
mainInputField.text = "Enter Text Here...";
}
// Update is called once per frame
void Update () {
}
}
But when i type: mainInputField.text then text is not exist.
'InputField' does not contain a definition for 'text' and no extension method 'text' accepting a first argument of type 'InputField' could be found (are you missing a using directive or an assembly reference?)
Answer by Hellium · Sep 29, 2017 at 11:38 AM
Because you have named your class the same way as Unity's, and yoir class does not have the member "text", you get this error.
Either rename your class or use the full name of the Unity'S InputField class :
private UnityEngine.UI.InputField mainInputField;
// ...
mainInputField = GetComponent <UnityEngine.UI.InputField>();
Answer by tormentoarmagedoom · Sep 29, 2017 at 12:03 PM
Good day.
Check this things:
Is this script a new script made by you? or you are trying to edit the InputField class?
You should'nt change the InputField class if dont know very good what you are doing, this class will be used for all InputField in your project. And if s a new script made by you, change the scrip name, because
Is the script at the same GameObject that contains the InputField? Using the sentence:
mainInputField = GetComponent<InputField>();
is correct, but is more correct to define where ther component must be searched, i mean in the gameObject that contains this script:
mainInputField = gameObject.GetComponent<InputField>();
Do you get some error when runing?
In this case try to debug the line where you get the error
Check this things and post the results, and i will try to help you again modyfiying this answer! :D
Bye :D :D
gameObject.GetComponent<InputField>();
is not "more correct" than
GetComponent<InputField>();
The first example is equal to
this.gameObject.GetComponent<InputField>();
while the second is equal to
this.GetComponent<InputField>();
this.gameObject
accesses the gameobject where this script is attached to. this.GetComponent
directly gets a component that is attached to the same gameobject where this script is attached to. So there's no difference in those two ways. It's just more complicated. So it's actually "less correct", just like doing:
gameObject.transform.transform.gameObject.transform.GetComponent<InputField>();
It would work just the same way but is completely pointless overhead.
Your answer
Follow this Question
Related Questions
How can i create array of texture2d with variables names ? 2 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers