Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity Crashes On Program Test

Discussion in 'Scripting' started by Technokid2000, Oct 31, 2017.

  1. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    Hi,

    I've been using Unity 3D for a while now, but for some reason, lately my scripts have been throwing up errors. Most have been fixed, but for some reason, one script refuses to run. The following script is designed to simulate a Bitcoin mining operation running on solar panels. However, when I run the script, Unity just immediately freezes up and has to be restarted.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SimScript : MonoBehaviour {
    7.     public float priceOfMiner;
    8.     public float priceOfPowerSupply;
    9.     public float priceOfRaspberryPi;
    10.     public float costOfShipping;
    11.     public float hashRateInTHs;
    12.     public int wattageOfPowerSupply;        //This value must be in watts, not Kilowatts
    13.  
    14.     public float costOfSolarSystem;
    15.     public int wattageOfSolarSystem;        //This value must be in watts, not Kilowatts
    16.  
    17.     public Text data;
    18.     public Text timeElapsed;
    19.     public float secondsUntilNextDay;
    20.  
    21.     public bool runDuringUpdate;
    22.     public bool runDuringStart;
    23.  
    24.     private float money;
    25.     private int numberOfMiners;
    26.     private int daysElapsed;
    27.     private int monthsElapsed;
    28.     private int yearsElapsed;
    29.     private int ampsTaken;
    30.     private int numberOfSolarSystems;
    31.     private int previousYear;
    32.  
    33.     void Start () {
    34.         numberOfMiners = 1;
    35.         numberOfSolarSystems = 1;
    36.         priceOfMiner += priceOfPowerSupply + costOfShipping;
    37.  
    38.         if (runDuringStart) {
    39.             StartCoroutine (simulate ());
    40.         }
    41.     }
    42.  
    43.     void Update () {
    44.         if (runDuringUpdate) {
    45.             StartCoroutine (simulate ());
    46.         }
    47.     }
    48.  
    49.     IEnumerator simulate () {
    50.         while (true) {
    51.             ampsTaken = numberOfMiners * wattageOfPowerSupply;
    52.             money += (7599.95f / 4.86f * hashRateInTHs) / 365 * numberOfMiners;
    53.             daysElapsed += 1;
    54.  
    55.             float variableForCalculations = daysElapsed / (30 + (1.25f / 3));
    56.             monthsElapsed = Mathf.FloorToInt (variableForCalculations);
    57.  
    58.             variableForCalculations = daysElapsed / 365;
    59.             yearsElapsed = Mathf.FloorToInt (variableForCalculations);
    60.  
    61.             if (!((numberOfMiners + 1) * wattageOfPowerSupply > wattageOfSolarSystem * numberOfSolarSystems)) {
    62.                 if (!(numberOfMiners % 20 == 0)) {
    63.                     if (money >= priceOfMiner) {
    64.                         numberOfMiners += 1;
    65.                         money -= priceOfMiner;
    66.                     }
    67.                 } else {
    68.                     if (money >= (priceOfMiner + priceOfRaspberryPi)) {
    69.                         numberOfMiners += 1;
    70.                         money -= (priceOfMiner + priceOfRaspberryPi);
    71.                     }
    72.                 }
    73.             } else {
    74.                 if (money >= costOfSolarSystem) {
    75.                     money -= costOfSolarSystem;
    76.                     numberOfSolarSystems += 1;
    77.                 }
    78.             }
    79.  
    80.             string sentence1 = "Number of miners = " + numberOfMiners + "\nTotal Hash Rate = " + (hashRateInTHs * numberOfMiners) + "\nTotal Number of Amps = " + ampsTaken + "\nNumber of Solar Systems = " + numberOfSolarSystems;
    81.             string sentence2 = "Days Elapsed: " + daysElapsed + "\nMonths Elapsed: " + monthsElapsed + "\nYears Elapsed: " + yearsElapsed;
    82.  
    83.             data.text = sentence1;
    84.             print ("Data Text Works!");
    85.             timeElapsed.text = sentence2;
    86.         }
    87.     }
    88. }
    I know that using a While loop is not recommended, hence is why I put it as a co-routine. I hope someone can help me please. It was literally working a few hours ago, and yet now it wont run. I haven't done a Unity update, so it couldn't have been that. It makes no sense...
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    You're missing a yield statement. Without one execution never leaves the while loop.
     
    Last edited: Oct 31, 2017
  3. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    I didn't think that would make a difference as A) that section of script is in a co-routine, and B) it is being looped via a while(true). I'll have to look into that.
     
  4. Technokid2000

    Technokid2000

    Joined:
    Dec 18, 2016
    Posts:
    36
    I got the script to work somehow. I had a copy of the original script in a word document and completely forgot about it. After some minor modifications and touch ups, I got it to run without any problems.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SimScript : MonoBehaviour {
    7.     public float priceOfMiner;
    8.     public float priceOfPowerSupply;
    9.     public float priceOfRaspberryPi;
    10.     public float costOfShipping;
    11.     public float hashRateInTHs;
    12.     public int wattageOfPowerSupply;        //This value must be in watts, not Kilowatts
    13.  
    14.     public float costOfSolarSystem;
    15.     public int wattageOfSolarSystem;        //This value must be in watts, not Kilowatts
    16.  
    17.     public Text data;
    18.     public Text timeElapsed;
    19.  
    20.     public bool waitForButton;
    21.     public float secondsUntilNextDay;
    22.  
    23.     private float money;
    24.     private int numberOfMiners;
    25.     private int daysElapsed;
    26.     private int monthsElapsed;
    27.     private int yearsElapsed;
    28.     private int ampsTaken;
    29.     private int numberOfSolarSystems;
    30.  
    31.     void Start () {
    32.         numberOfMiners = 1;
    33.         numberOfSolarSystems = 1;
    34.         priceOfMiner += priceOfPowerSupply + costOfShipping;
    35.         StartCoroutine (simulate ());
    36.     }
    37.  
    38.     IEnumerator simulate () {
    39.         while (true) {
    40.             ampsTaken = numberOfMiners * wattageOfPowerSupply;
    41.             if (yearsElapsed == 0) {
    42.                 money += (7599.95f / 4.86f * hashRateInTHs) / 365 * numberOfMiners;
    43.             } else {
    44.                 money += (7599.95f / 4.86f * hashRateInTHs) / 365 * numberOfMiners * 0.5f;
    45.             }
    46.             print (money);
    47.             daysElapsed +=1;
    48.             monthsElapsed = Mathf.FloorToInt (daysElapsed / (30 + 1.25f / 3));
    49.             yearsElapsed = Mathf.FloorToInt (daysElapsed / 365);
    50.  
    51.             if (!((numberOfMiners + 1) * wattageOfPowerSupply > wattageOfSolarSystem * numberOfSolarSystems)) {
    52.                 if (!(numberOfMiners % 20 == 0)) {
    53.                     if (money >= priceOfMiner) {
    54.                         numberOfMiners += 1;
    55.                         money -= priceOfMiner;
    56.                     }
    57.                 } else {
    58.                     if (money >= (priceOfMiner + priceOfRaspberryPi)) {
    59.                         numberOfMiners += 1;
    60.                         money -= (priceOfMiner + priceOfRaspberryPi);
    61.                     }
    62.                 }
    63.             } else {
    64.                 if (money >= costOfSolarSystem) {
    65.                     money -= costOfSolarSystem;
    66.                     numberOfSolarSystems += 1;
    67.                 }
    68.             }
    69.  
    70.             string sentence1 = "Number of miners = " + numberOfMiners + "\nTotal Hash Rate = " + (hashRateInTHs * numberOfMiners) + "\nTotal Number of Amps = " + ampsTaken + "\nNumber of Solar Systems = " + numberOfSolarSystems;
    71.  
    72.             string sentence2 = "Days Elapsed: " + daysElapsed + "\nMonths Elapsed: " + monthsElapsed + "\nYears Elapsed: " + yearsElapsed;
    73.  
    74.             data.text = sentence1;
    75.             timeElapsed.text = sentence2;
    76.  
    77.             if (!waitForButton) {
    78.                 yield return new WaitForSeconds (secondsUntilNextDay);
    79.             } else {
    80.                 while (!(Input.GetMouseButtonDown (0))) {
    81.                     print("Waiting for user…");
    82.                 }
    83.             }
    84.         }
    85.     }
    86.  
    87. }
    88.