Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 N8_TGK · Jun 27, 2021 at 02:20 AM · 2d gamecardboardgame

Linking strings and integers?

Okay so I have a bit of an issue. I currently have this, but it's not what I want. Current system is that you draw the card, read what it says, then manually add or remove points using buttons (irrelevant so not included in my code here). I want it automatic, so basically what I want to happen is when you click a card it displays the text, then it needs to give points directly related to what was declared in the string. If the string element that's called says to add 6 points, it should then automatically add 6 points. I don't know how to do this with strings, arrays, dictionaries, or lists. Obviously I can't declare an integer or float as to what pointamount in the string, so somehow I think I need a seperate string or integers that would get called at the same time as the string, but would be linked and be specific to that string element. Someone please help me. Here's my current code:

 public class Game : MonoBehaviour
 {
 
     public int PointCount;
     public Text PointText;
     public GameObject CardDisplayBox;
     public TMP_Text CardText;
     public GameObject CardButton;
     public float pointCount;
     #region Cards List
 
     string[] cards = { "RANDOM CARD 1. +3 Points", "RANDOM CARD 2. +2 points", "RANDOM CARD 3. +6 points " };
 
     #endregion
 
 
 
     #region Card Display
     public void OnCardClick()
     {
         CardDisplayBox.SetActive(true);
         SetCard();
     }
 
     private void SetCard()
     {
 
         string randomCard = cards[Random.Range(0, cards.Length)];
 
         CardText.text = randomCard;
            
         
     }
     #endregion
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

Answer by Namey5 · Jun 27, 2021 at 06:17 AM

This is where object-oriented programming comes in - you can create a 'Card' object that contains both the display text and points to add, then select from a list of cards rather than strings;

 [System.Serializable]
 public struct Card
 {
     string text;
     int points;
 
     public Card (string a_Text, int a_Points)
     {
         text = a_Text;
         points = a_Points;
     }
 }
 
 ...
 
 Card[] cards = 
 {
     new Card ("RANDOM CARD 1. +3 Points", 3),
     new Card ("RANDOM CARD 2. +2 Points", 2),
     new Card ("RANDOM CARD 3. +6 Points", 6)
 };
 
 private void SetCard ()
 {
     Card randomCard = cards[Random.Range (0, cards.Length)];
     // Then, just access the members of the card
     CardText.text = randomCard.text;
     AddPoints (randomCard.points);
 }
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

162 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 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 draw border around uneven board like shown in the picture. 0 Answers

Changing array of display images with other set of images on user button press event 0 Answers

Is this the right program 0 Answers

Unity equivalent of GeekGameBoard? 3 Answers

How to get object to follow a curved path to reach a moving destination? 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