Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Retrieve the Unity object to C# script

Discussion in 'Getting Started' started by danchello, May 16, 2020.

  1. danchello

    danchello

    Joined:
    Jul 22, 2017
    Posts:
    2
    Hi,

    I'm the very beginner.
    I have an object (Input field) that has a name "InputPhone" and I need to get the text from it in my C# script. This script is run by the push button. I googled that I'm able to do this via command:
    Code (CSharp):
    1. GameObject.Find("InputPhone").GetComponent<Text>();
    But this returns me nothing (but still I can launch the app without compiler errors).

    Here is my full code that users can run by clicking this push button:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Net;
    5. using System.Net.Mail;
    6. using System.Net.Security;
    7. using System.Security.Cryptography.X509Certificates;
    8. using UnityEngine.UI;
    9.  
    10.  
    11. public class Send : MonoBehaviour
    12. {
    13.     public void send()
    14.     {
    15.         MailMessage mail = new MailMessage();
    16.         mail.From = new MailAddress("example@mail.com");
    17.         mail.To.Add("example@mail.com");
    18.         mail.Subject = "Customer";
    19.         mail.Body = "Datas:" + GameObject.Find("inputPhone").GetComponent<Text>(); // here I use GameObject.Find();
    20.         SmtpClient smtpServer = new SmtpClient("smtp.yandex.ru");
    21.         smtpServer.Port = 587;
    22.         smtpServer.Credentials = new System.Net.NetworkCredential("example@mail.com", "Password") as ICredentialsByHost;
    23.         smtpServer.EnableSsl = true;
    24.         ServicePointManager.ServerCertificateValidationCallback =
    25.         delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    26.         { return true; };
    27.         smtpServer.Send(mail);
    28.     }
    29. }
    30.  
    Here is the screen of my application:
     

    Attached Files:

    • 1111.png
      1111.png
      File size:
      128.2 KB
      Views:
      20
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Check your spelling/case. Line #19 should be;

    mail.Body = "Datas:" + GameObject.Find("InputPhone").GetComponent<Text>(); // here I use GameObject.Find();
     
  3. danchello

    danchello

    Joined:
    Jul 22, 2017
    Posts:
    2
    Do you mean inputPhone -> InputPhone?
    Yeah I already did it right, but it still does not work.
    The problem is that I get mail, but I dont receive text from input field.
    And I still dont understand why.
     
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Yes.

    Once I saw your first mistake I stopped looking. So your code is finding the component Text, but not the actual text. Try this;

    mail.Body = "Datas:" + GameObject.Find("InputPhone").GetComponent<Text>().text;
     
unityunity