Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 bodn10 · Feb 09, 2019 at 10:13 AM · textintintegerprinttostring

how can I print to text the number of an int as well as the name of the int.

I am using a point system that has categories of points. I am looking for a way to access both the value of an int and the name of the int to be printed on what will ultimately be a "high score" page. Not sure if I am approaching with the right angle on this problem.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Int_SortScript : MonoBehaviour
 {
     public Text Project;

     public int junk;
     public int trash;
     public int waste;
     public int prize;
     
 
     public int PrintMax;
 
     public List<int> PointList = new List<int>();
     
     int GetMax()
     {
         PointList.Sort();
         int max = PointList[PointList.Count - 1];
         return max;
     }
     
     void Start()
     {
         PointList.Add(junk);
         PointList.Add(trash);
         PointList.Add(waste);
         PointList.Add(prize);
     }
 
     
     void Update()
     {
         PrintMax = GetMax();
         Project.text = PrintMax.ToString();
     }
 }
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

1 Reply

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

Answer by Hellium · Feb 09, 2019 at 10:42 AM

Once you put the integers in the array/list, you can't retrieve its name anymore.

Try the following codes:

 public class Int_SortScript : MonoBehaviour
 {
     public Text Project;
     public int junk;
     public int trash;
     public int waste;
     public int prize;
     
     private string GetMax()
     {
         if( junk == Mathf.Max( junk, trash, waste, prize ) ) return junk + " " + nameof( junk ) ; 
         if( trash == Mathf.Max( junk, trash, waste, prize ) ) return trash + " " + nameof( trash ) ; 
         if( waste == Mathf.Max( junk, trash, waste, prize ) ) return waste + " " + nameof( waste ) ; 
         if( prize == Mathf.Max( junk, trash, waste, prize ) ) return prize + " " + nameof( prize ) ;
     }
     
     void Update()
     {
         Project.text = GetMax();
     }
 }


 public class Int_SortScript : MonoBehaviour
 {
     public Text Project;
     public int junk;
     public int trash;
     public int waste;
     public int prize;
     
     private static Dictionary<string, int> values = new Dictionary<string, int>();

     private string GetMax()
     {
         values.Clear();
         
         values.Add( nameof( junk ), junk ) ;
         values.Add( nameof( trash ), trash ) ;
         values.Add( nameof( waste ), waste ) ;
         values.Add( nameof( prize ), prize ) ;
         
         int max = -1;
         string maxName = null ;
         foreach( KeyValuePair<string, int> v in values )
         {
             if( v.Value > max )
             {
                 max = v.Value;
                 maxName = v.Key;
             }
         }
         
         return max + " " + maxName ;
     }
     
     void Update()
     {
         Project.text = GetMax();
     }
 }
Comment
Add comment · Show 3 · 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 bodn10 · Feb 09, 2019 at 02:06 PM 0
Share

Great! Thanks for the reply! The first one worked like a charm once I returned getmax. I would like the second one to work because that layout works for me better. I would then be able to add more ints if need be in the future. Its co$$anonymous$$g up with a missing return of a object reference which I don't understand. Would you be able to explain that too me?

alt text

capture.png (119.5 kB)
avatar image Hellium bodn10 · Feb 09, 2019 at 03:14 PM 0
Share

$$anonymous$$y bad, I fixed the code (removed static keyword + adding type to $$anonymous$$eyValuePair)

avatar image bodn10 Hellium · Feb 09, 2019 at 08:40 PM 0
Share

Oh! that makes sense. Thank you. this works. I understand.

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

103 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

Related Questions

UI system multiple ints as function arguments 4 Answers

How to make an int not pass 0 2 Answers

Script not changing int variable 2 Answers

Take an int from text and equal it to another text 0 Answers

Checking to see if a loaded level is equal to a member of an integer array 1 Answer


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