Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by Kirderf · Jul 11, 2015 at 02:03 PM · c#textrandommatching

How can I match and display two random numbers using C#?

This "should" be easy, but I have been at it all day long. Can anyone PLEASE help me?

I am trying to create two random numbers, display them on the page, and turn one red when there is a match. I must be doing something very wrong, because I can not get the page to display a match. (See this image??) alt text

The layout is has two text ojbects called TextBox1 and TextBox2, each has the "gm.cs" linked to it. There is one button on the page which calls the three functions(getText1, getText2, and clickStart) onClick. It could be something very simple, or I could be doing this all wrong. Any help would be greatly appreciated.
alt text

Here is the code for gm.cs

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class gm : MonoBehaviour {
     public int text1;
     public int text2;
     public Text txt1;
     public Text txt2;
 
     public void getText1 () {
         text1 = Random.Range (0, 9);
         Debug.Log (text1);
         txt1 = GetComponent<Text> ();
         txt1.text = (text1.ToString ("0"));
     }
     
     public void getText2 () {
         text2 = Random.Range (0, 9);
         Debug.Log (text2);
         txt2 = GetComponent<Text>();
         txt2.text = (text2.ToString("0"));    
     }
 
     public void clickStart() {        
         if (text1 == text2) {
                         txt1.color = Color.red;
             Debug.Log ("We have a match!");
         } else {
             Debug.Log ("nuthin");
         }
     }
 }


example.png (26.0 kB)
example2.png (61.1 kB)
Comment
Add comment
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

2 Replies

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

Answer by barbe63 · Jul 11, 2015 at 02:42 PM

What's probably happening is you are calling clickStart before getText1 and getText2.

I also see problem with those lines that should not exist:

 txt1 = GetComponent<Text> ();
 

You probably assign txt1 and txt2 to the inspector so they don't need it.

What I propose is this, instead of calling the getText1 and 2 from again probably the button do this:

      public void clickStart() {  
          getText1();
          getText2();      
          if (text1 == text2) {
                          txt1.color = Color.red;
              Debug.Log ("We have a match!");
          } else {
              Debug.Log ("nuthin");
          }
      }



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
avatar image
0

Answer by Kirderf · Jul 12, 2015 at 09:27 AM

Yayyyy!!! Wow, this worked perfectly.

I added the gm.cs script to an empty object in the Heirarchy and, using the button, onClick, I called the clickStart function. The txt1 and txt2 are also attached to each textbox. (See the image below)

Thank you soooo much for taking a look at this!!

alt text


example3.png (89.2 kB)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

To see chosen random text UI Unity,To Access UI text 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

using Switch and Case for 50% chance of occurance? C# 3 Answers

Teleporting camera and gameobjekt togather at random? 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