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 /
avatar image
0
Question by Leon123442 · Aug 25, 2018 at 02:07 PM · scripting problemscript.variablevariablesvalue

How can I improve my trading script?

I am making a simple traiding script, were the player can buy and sell products in different towns. I am using UI buttons and elements for most aktions, but the script is getting really long and it is a lit if work to write all the wariables again and again for every city. Is there a way to improve the code?

Here is my script so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class InventoryManager : MonoBehaviour {
     //textelements
     public Text Cointext;
         //playergoods
     public Text sakeplayer;
     public Text fishplayer;
     public Text toolsplayer;
     public Text ironoreplayer;
     public Text woodplayer;
 
     //Player
     public float sake = 0f;
     public float fish = 0f;
     public float tools = 0f;
     public float ironore = 0f;
     public float wood = 0f;
 
     public float coins = 100;
 
     //Town1
     //Produces Sake and Tools needs Wood and Iron
     public float sakeT1; //p
     public float fishT1;
     public float toolsT1; //p
     public float ironoreT1; //n
     public float woodT1; //n
 
     public float sakepriceT1 = 5f;
     public float fishpriceT1 = 10f;
     public float toolspriceT1 = 25f;
     public float ironorepriceT1 = 15f;
     public float woodpriceT1 = 15f;
 
     public float sakesellpriceT1 = 3f;
     public float fishsellpriceT1 = 7f;
     public float toolssellpriceT1 = 15f;
     public float ironoresellpriceT1 = 10f;
     public float woodsellpriceT1 = 10f;
 
     void Start() {
         //playertext
         Cointext.GetComponent<Text>();
         sakeplayer.GetComponent<Text>();
         fishplayer.GetComponent<Text>();
         toolsplayer.GetComponent<Text>();
         ironoreplayer.GetComponent<Text>();
         woodplayer.GetComponent<Text>();
         //town1
     }
 
     void Update () {
         //playertext
         Cointext.text = coins.ToString();
         sakeplayer.text = sake.ToString();
         fishplayer.text = fish.ToString();
         toolsplayer.text = tools.ToString();
         ironoreplayer.text = ironore.ToString();
         woodplayer.text = wood.ToString();
     }
     //Town1
     public void BsakeT1()
     {
         if (sakeT1 >= 1)
         {
             sakeT1 -= 1;
             sake += 1;
             coins -= sakepriceT1;
         }
     }
     public void SsakeT1()
     {
         if (sake >= 1)
         {
             sakeT1 += 1;
             sake -= 1;
             coins += sakesellpriceT1;
         }
     }
     public void BfishT1()
     {
         if (fishT1 >= 1)
         {
             fishT1 -= 1;
             fish += 1;
             coins -= fishpriceT1;
         }
     }
     public void SfishT1()
     {
         if (fish >= 1)
         {
             fishT1 += 1;
             fish -= 1;
             coins += fishsellpriceT1;
         }
     }
     public void BtoolsT1()
     {
         if (toolsT1 >= 1)
         {
             toolsT1 -= 1;
             tools += 1;
             coins -= toolspriceT1;
         }
     }
     public void StoolsT1()
     {
         if (tools >= 1)
         {
             toolsT1 += 1;
             tools -= 1;
             coins += toolssellpriceT1;
         }
     }
     public void BironoreT1()
     {
         if (ironoreT1 >= 1)
         {
             ironoreT1 -= 1;
             ironore += 1;
             coins -= ironorepriceT1;
         }
     }
     public void SironoreT1()
     {
         if (ironore >= 1)
         {
             ironoreT1 += 1;
             ironore -= 1;
             coins += ironoresellpriceT1;
         }
     }
     public void BwoodT1()
     {
         if (woodT1 >= 1)
         {
             woodT1 -= 1;
             wood += 1;
             coins -= woodpriceT1;
         }
     }
     public void SwoodT1()
     {
         if (wood >= 1)
         {
             woodT1 += 1;
             wood -= 1;
             coins += woodsellpriceT1;
         }
     }
 }

`

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 eses · Aug 25, 2018 at 02:46 PM 0
Share

@ Leon123442 - Very few have eyes to read that... maybe format your code with code block?

avatar image tormentoarmagedoom eses · Aug 25, 2018 at 04:12 PM 0
Share

changed format.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · Aug 25, 2018 at 04:18 PM

Good day.

You should make a general script to make all transactions. Create a general method to do it, using strings to access the variable you need:


if...

 string something = "Wood";
 float someValue = 21;


 methodName(something, someValue);


 void methodName (string Resource, float Cost)
 {
  if (GetType().GetField(Resource).GetValue() >= Cost)
          {
             GetType().GetField(Resource+"T1").GetValue() += Cost;
              GetType().GetField(Resource).GetValue() -= Cost;
              coins += woodsellpriceT1;
          }
 }

I did not check the logic of the code, is just an example to show you how to replace each variable for a general variable that reads / edit (with SetValue) the values of other variables.

Go read, wach and learn about it.

Good luck!

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

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

Related Questions

non-static Variable in script on multiple gameObjects reset. Why? 1 Answer

How to READ a variable VALUE from other Object Script? 1 Answer

Changing a public varible from another script 3 Answers

C# taking variable from other script 2 Answers

Variables modified on other scripts through a Editor Script reset on Play? 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