Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question My script crashes Unity on playtest

Discussion in 'Scripting' started by Gabriel_wi, Jun 9, 2023.

  1. Gabriel_wi

    Gabriel_wi

    Joined:
    Nov 14, 2022
    Posts:
    2
    Hey, I just started learning C# this year and I'm trying to work on a skill progression calculator sort of thing as an exercise/to do some math for me. However, to my surprise, attaching this script to a gameObject and hitting 'Playtest' leaves me to a full freeze of Unity as though I had sent it into a loop. I think I must be missing something very simple here that's letting it go unhinged. Here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SkillProgression : MonoBehaviour
    6. {
    7.     public int clairvoyance = 0;
    8.     public int empathy = 0;
    9.     public int desolation = 0;
    10.     public int maxLevel = 99;
    11.  
    12.     public string[] mainQuestsInChapter;
    13.     public string[] sideQuestsInChapter;
    14.     public int numberOfChapters = 4;
    15.  
    16.     public int maxPointsAllottedPerMainquest;
    17.     public int maxPointsAllottedPerSidequest;
    18.  
    19.  
    20.     // Start is called before the first frame update
    21.     void Awake()
    22.     {
    23.         Calculate();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.      
    30.     }
    31.  
    32.     void Calculate()
    33.     {
    34.         Debug.Log("Starting Clairvoyance value is " + clairvoyance + ".");
    35.         Debug.Log("Starting Empathy value is " + empathy + ".");
    36.         Debug.Log("Starting Desolation value is " + desolation + ".");
    37.  
    38.         Debug.Log("You did " + mainQuestsInChapter.Length + " main quests in Chapter 1.");
    39.         int rewardedPoints = mainQuestsInChapter.Length * maxPointsAllottedPerMainquest;
    40.         Debug.Log("Playing through perfectly, you earned " + rewardedPoints + " points. Time for them to get assigned!");
    41.         float pointsAverage = rewardedPoints / 3;
    42.         Debug.Log("" + pointsAverage + " is the float pointsAverage.");
    43.  
    44.         for(int i = 0; i < mainQuestsInChapter.Length; i++)
    45.         {
    46.             for (int e = 0; i < pointsAverage; e++)
    47.             {
    48.                 int randomSkilldist = (Random.Range(1, 3));
    49.                 Debug.Log("Random Skill chosen to level is " + randomSkilldist);
    50.                 if (randomSkilldist == 1)
    51.                 {
    52.                     clairvoyance = clairvoyance + 1;
    53.                     Debug.Log("Clairvoyance is " + clairvoyance);
    54.                 }
    55.                 else if (randomSkilldist == 2)
    56.                 {
    57.                     empathy = empathy + 1;
    58.                     Debug.Log("Empathy is " + empathy);
    59.                 }
    60.                 else if (randomSkilldist == 3)
    61.                 {
    62.                     desolation = desolation + 1;
    63.                     Debug.Log("Desolation is " + desolation);
    64.                 }
    65.             }
    66.         }
    67.         Debug.Log("Final build is CLAIRVOYANCE: " + clairvoyance + "EMPATHY: " + empathy + "DESOLATION: " + desolation);
    68.     }
    69. }
    70.  
    The goal is that for every mainQuestsinChapter, the number of 'pointsAverage' get randomly distributed to 1 of 3 the three skills to then spit me out a 'build' at the end. I currently haven't written a part to utilize the 'MaxLevel' or 'MaxPointsAllottedPerSidequest' integers, or the 'sideQuestsInChapter' string array.

    EDIT: the values I have been feeding it before hitting playtest are:
     
    Last edited: Jun 9, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    First step is to debug it. Attach a debugger and step through it. I'm curious how you even know it's the above code if you've not done this, it could be anything really.

    Nobody else can debug it for you. :)
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    As mentioned, debug your code.

    But, you seem to at least suggest the possibility of an infinite loop. So, that's always a good place to start. Check any looping you're doing and see if they actually do end. With that in mind, your two for loops might not be correct. Are you sure i is what you want for the second for loop? I didn't look closely at what you're doing, so maybe it is. But I would start there.

    upload_2023-6-9_10-3-56.png
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    This doesn't seem correct, specifically the i part
     
    MelvMay likes this.