Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by JohnsonGames · Nov 09, 2017 at 10:40 PM · unity 5unityeditor

how to convert text to string

I want to convert a UI Text to a string, so I can see if it is a correct answer.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class car : MonoBehaviour {
 
     public GameObject fordimage;
     public GameObject chevyimage;
     public GameObject jaguarimage;
     public GameObject mazdaimage;
     public GameObject toyotaimage;
     public int counter;
     public Text answer;
     public string correctanswer;
     public Button nextbutton;
 
     void Start () 
     {
         correctanswer = ("");
         fordimage.SetActive (false);
         chevyimage.SetActive (false);
         jaguarimage.SetActive (false);
         mazdaimage.SetActive (false);
         toyotaimage.SetActive (false);
         counter = Random.Range (1,5);
         nextbutton.interactable = false;
         print (counter);
     }
 
 
     void Update () 
     {
         if (answer.ToString() == correctanswer) //error on this line
         {
             nextbutton.interactable = true;
         }
 
 
         if (counter == 1) 
         {
             fordimage.SetActive (true);
             correctanswer = ("ford");
         }
         else 
         {
             fordimage.SetActive (false);
         }
         if (counter == 2) 
         {
             chevyimage.SetActive (true);
             correctanswer = ("chevy");
         } 
         else 
         {
             chevyimage.SetActive (false);
         }
         if (counter == 3)
         {
             jaguarimage.SetActive (true);
             correctanswer = ("jaguar");
         }
         else
         {
             jaguarimage.SetActive (false);
         }
         if (counter == 4)
         {
             mazdaimage.SetActive (true);
             correctanswer = ("mazda");
         }
         else
         {
             mazdaimage.SetActive (false);
         }
         if (counter == 5)
         {
             toyotaimage.SetActive (true);
             correctanswer = ("toyota");
         }
         else
         {
             toyotaimage.SetActive (false);
         }
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JohnsonGames · Nov 10, 2017 at 02:16 PM 0
Share

So I have a input field, and I am trying to get the text that the player inputs.

avatar image Dragate JohnsonGames · Nov 10, 2017 at 02:19 PM 0
Share

https://docs.unity3d.com/ScriptReference/UI.InputField-text.html

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JohnsonGames · Nov 10, 2017 at 02:41 PM

Found the Answer:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class car : MonoBehaviour {
 
     public GameObject fordimage;
     public GameObject chevyimage;
     public GameObject jaguarimage;
     public GameObject mazdaimage;
     public GameObject toyotaimage;
     public int counter;
     public InputField answer;
     public string correctanswer;
     public Button nextbutton;
     public Button pausebutton;
 
     void Start () 
     {
         nextbutton.interactable = false;
         correctanswer = ("None");
         fordimage.SetActive (false);
         chevyimage.SetActive (false);
         jaguarimage.SetActive (false);
         mazdaimage.SetActive (false);
         toyotaimage.SetActive (false);
         counter = Random.Range (1,6);
         print (counter);
         Button next1 = nextbutton.GetComponent<Button> ();
         next1.onClick.AddListener (clicked);
     }
 
     void clicked ()
     {
         correctanswer = ("None");
         counter = Random.Range (1,6);
         nextbutton.interactable = false;
     }
 
     void Update () 
     {
         if (answer.text == correctanswer)
         {
             nextbutton.interactable = true;
         }
 
         if (counter == 1) 
         {
             fordimage.SetActive (true);
             correctanswer = ("ford");
         }
         else 
         {
             fordimage.SetActive (false);
         }
         if (counter == 2) 
         {
             chevyimage.SetActive (true);
             correctanswer = ("chevy");
         } 
         else 
         {
             chevyimage.SetActive (false);
         }
         if (counter == 3)
         {
             jaguarimage.SetActive (true);
             correctanswer = ("jaguar");
         }
         else
         {
             jaguarimage.SetActive (false);
         }
         if (counter == 4)
         {
             mazdaimage.SetActive (true);
             correctanswer = ("mazda");
         }
         else
         {
             mazdaimage.SetActive (false);
         }
         if (counter == 5)
         {
             toyotaimage.SetActive (true);
             correctanswer = ("toyota");
         }
         else
         {
             toyotaimage.SetActive (false);
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 10, 2017 at 05:22 PM 1
Share

You "Found the Answer"? You mean smkmth posted the answer 18 hours ago...

avatar image
3

Answer by smkmth · Nov 09, 2017 at 10:48 PM

answer.text is how you access the text component of a text game object

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Leonstar0 · Nov 10, 2017 at 07:04 AM 0
Share

To elaborate: UI Text is an object which handles the rendering of a string (because strings are just data that represent a sequence of characters, it doesn't care how it should display that data (like fonts, colour, etc.)). An instance of UI Text will store the string that it is responsible for rendering in the "text" property of the object, (which as noted in the answers, is accessible through [insert_name_of_ui_text_instance].text).

avatar image
1

Answer by TarahPeltz · Nov 10, 2017 at 02:26 AM

@smkmth is correct, use answer.text. Here is the Unity Documentation for this: https://docs.unity3d.com/ScriptReference/UI.Text-text.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to load level after typing somthing? 1 Answer

Running shell scripts Mac: POST BUILD -1 Answers

Unity Game View support Touch Events? 0 Answers

creating project folder failed!,creating project folder failed! 5 Answers

Unity UI Test Automation displays error, after compiling 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges