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 Awsz · Dec 04, 2016 at 09:19 PM · c#percentage

Percentage calculation

Hi, im working on a project.

I'm having an issue with a script calculating the percentage. I kind of works, but not really, if you understand. here is my script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class spawner : MonoBehaviour {
     public Transform LeftSpawn;
     public Transform RightSpawn;
 
     public GameObject LeftCube;
     public GameObject RightCube;
 
     public Text LeftText;
     public Text RightText;
 
     public int LeftCount;
     public int RightCount;
     public int TotalCount;
 
     public float LeftFraction;
     public float RightFraction;
     public float LeftPercentage;
     public float RightPercentage;
 
 
 
     // Use this for initialization
     void Start () {
         LeftCount = 0;
         RightCount = 0;
         TotalCount = 0;
     
     }
     
     // Update is called once per frame
     void Update () {
         // if both right and left have at least 1 cube each
         if (LeftCount != 0 && RightCount != 0) {
             //the total amount of cubes
             TotalCount = LeftCount + RightCount;
             // how much out of the total amount is left?
             LeftFraction = TotalCount / LeftCount;
             //how much out of the total amount is right?
             RightFraction = TotalCount / RightCount;
             //calculate how much (in percentage) there are left cubes
             LeftPercentage = 100 / LeftFraction;
             // same with right cubes
             RightPercentage = 100 / RightFraction;
             // show the percentage
             LeftText.text = LeftPercentage.ToString () + "%";
             //ditto
             RightText.text = RightPercentage.ToString () + "%";
         }
 
 
             
         
     
     
         // if "a" is pressed
         if (Input.GetKeyDown (KeyCode.A)) {
             // spawn cube at designated transform
             Instantiate (LeftCube, LeftSpawn);
             // add one to leftcount and totalcount, confirm everything happenned
             LeftCount += 1;
             TotalCount += 1;
             Debug.Log ("done");
 
         }
         //if "d" is pressed
         if (Input.GetKeyDown (KeyCode.D)) {
             //spawn cube at designated transform
             Instantiate (RightCube, RightSpawn);
             // add one to rightcount and total count, confirm it happenned
             RightCount += 1;
             TotalCount += 1;
             Debug.Log ("done");
         }
     }
     
 }
 

here is my script in action: alt text

here, the leftcube to rightcube ratio is 5:2

the script only works if both sides have an equal number squares, putting "50%" on each side.

Can someone help me?

unity.png (104.0 kB)
Comment
Add comment · Show 1
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 kingkoby48 · Jan 12, 2021 at 08:16 AM 0
Share

@Awsz Hi I'm actually trying to accomplish a similar thing to you right now, by any chance could you help me?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Dec 04, 2016 at 09:28 PM

what about this equation:

text = LeftCount / TotalCount * 100;

same for right

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 Awsz · Dec 07, 2016 at 11:56 AM 0
Share

thanks, I knew there was an easier method.

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

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

Two stupid questions about calling a method from another script (C#) 2 Answers

OnClickListener added but on vacation? 0 Answers

Can I AddClip() to Animation component during GameLoop? 0 Answers

warring cs0414 1 Answer

How can I autofire a ships gun every half second C# 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