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

Unity Crash on Input(SOLVED)

Discussion in 'Editor & General Support' started by kingbas33, Sep 9, 2014.

  1. kingbas33

    kingbas33

    Joined:
    Sep 9, 2014
    Posts:
    9
    Hi,

    I'm currently working on a project for a Mastery/Skill tree system.
    When i hit the play button everything works fine until you give an input to Unity, which is required for the "game" to do what it's supposed to do. It crashes unity. This happens with both mouse and keyboard input but only when it has a programmed function. Is there an error in my code or is it related to the mac version of Unity?

    I'm not very good with C# yet and this is the first project if worked out myself, and i know this is not the most efficient way of doing it.

    Below is the code i'm using:

    Mastery_Manager Classe:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Mastery_Manager : MonoBehaviour
    6. {
    7.     public int availablePoints;
    8.     public int level;
    9.    
    10.     public List<IMastery> Masteries = new List<IMastery>();
    11.     public List<IMastery> UnlockedMasteries = new List<IMastery>();
    12.     public Texture2D defenderIconAvailable;
    13.     public Texture2D defenderIconNotAvailable;
    14.    
    15.     IMastery Defender;
    16.    
    17.     // Use this for initialization
    18.     void Awake () {
    19.         level = 1;
    20.         availablePoints = 10;
    21.         Defender = new Mastery_Defender();
    22.         Defender.Position = new Rect(100, 100, 64, 64);
    23.         Defender.IconAvailable = defenderIconAvailable;
    24.         Defender.IconNotAvailable = defenderIconNotAvailable;
    25.         Masteries.Add(Defender);
    26.  
    27.  
    28.         foreach (IMastery mastery in Masteries) {
    29.             mastery.Start();
    30.         }
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update () {
    35.         foreach (IMastery mastery in Masteries) {
    36.             mastery.Update();
    37.         }
    38.  
    39.         if (Input.GetKeyDown(KeyCode.Space)) {
    40.             AddPoint();
    41.         }
    42.  
    43.         if (Input.GetKeyDown(KeyCode.L)) {
    44.             LevelUp();
    45.         }
    46.     }
    47.  
    48.  
    49.     void OnGUI () {
    50.         foreach (IMastery mastery in Masteries) {
    51.             mastery.Draw();
    52.         }
    53.  
    54.         GUI.Label(new Rect(100, 200, 500, 100), "Press Space to add a point , press L to level up");
    55.     }
    56.  
    57.     public void CheckIfMasteriesAreAvailable() {
    58.         foreach (IMastery mastery in Masteries) {
    59.             mastery.Available = mastery.IsAvailable();
    60.         }
    61.     }
    62.  
    63.     private void LevelUp() {
    64.         level++;
    65.         CheckIfMasteriesAreAvailable();
    66.     }
    67.  
    68.     private void AddPoint() {
    69.         availablePoints++;
    70.         CheckIfMasteriesAreAvailable();
    71.     }
    72. }
    Mastery_Defender Class:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Mastery_Defender : IMastery {
    6.  
    7.     private string masteryName;
    8.     private string masteryDescription;
    9.    
    10.     private bool available;
    11.     private bool pointsAdded;
    12.    
    13.     private int currentPointsAdded;
    14.     private int maxPointsAdded;
    15.     private int requiredLevel;
    16.    
    17.     private Rect position;
    18.    
    19.     private Texture2D iconAvailable;
    20.     private Texture2D iconNotAvailable;
    21.  
    22.     private GameObject mainCamera;
    23.     private Mastery_Manager masteryManager;
    24.    
    25.    
    26.     private List<IMastery> requiredMasteries = new List<IMastery>(); //only if mastery has requirements
    27.  
    28.     // Use this for initialization
    29.     public void Start () {
    30.  
    31.         mainCamera = GameObject.Find("Main Camera");
    32.         masteryManager = (Mastery_Manager)mainCamera.GetComponent("Mastery_Manager");
    33.        
    34.         masteryName = "Defender";
    35.         masteryDescription = "Gives a small boost to your defensive stats.";
    36.         currentPointsAdded = 0;
    37.         maxPointsAdded = 3;
    38.         requiredLevel = 2;
    39.         available = IsAvailable ();
    40.  
    41.  
    42.  
    43.  
    44.  
    45.     }
    46.    
    47.     // Update is called once per frame
    48.     public void Update () {
    49.     }
    50.  
    51.     public bool IsAvailable() {
    52.         //Check if this mastery is available for useage i.e. points available, requiredmasteries unlocked
    53.         if (AreRequiredMasteriesUnlocked()) {
    54.             if (masteryManager.level >= requiredLevel) {
    55.                 if (masteryManager.availablePoints > 0) {
    56.                     Debug.Log (masteryName + " is available");
    57.                     return true;
    58.                 }
    59.                 else {
    60.                     Debug.Log (masteryName + " is unavailable");
    61.                     return false;
    62.                 }
    63.             }
    64.             else {
    65.                 Debug.Log (masteryName + " is unavailable");
    66.                 return false;
    67.             }
    68.         }
    69.         else {
    70.             Debug.Log (masteryName + " is unavailable");
    71.             return false;
    72.         }
    73.     }
    74.  
    75.     public void OnLeftClick() {
    76.         //Left Click Behaviour
    77.         //Add 1 point to current points if < Maxpoints
    78.         if (currentPointsAdded < maxPointsAdded) {
    79.                 currentPointsAdded++;
    80.                 masteryManager.availablePoints--;
    81.                 Debug.Log ("A point has been added to: " + masteryName);
    82.                 pointsAdded = true;
    83.                 masteryManager.CheckIfMasteriesAreAvailable();
    84.         }
    85.     }
    86.  
    87.     public void OnRightClick() {
    88.         //Right Click Behaviour
    89.         //Substract 1 point from current points if > 0
    90.         if (currentPointsAdded > 0) {
    91.             currentPointsAdded --;
    92.             masteryManager.availablePoints++;
    93.             Debug.Log("A point has been substracted from: " + masteryName);
    94.             if (currentPointsAdded == 0) {
    95.                 pointsAdded = false;
    96.             }
    97.             masteryManager.CheckIfMasteriesAreAvailable();
    98.         }
    99.     }
    100.  
    101.  
    102.     public void Draw() {
    103.         //Draw either Available or Not available Icon on given position
    104.         if (available) {
    105.             if (GUI.Button(position, iconAvailable, "label")) {
    106.                 if (Event.current.button == 0) {
    107.                     OnLeftClick();
    108.                 }
    109.                 else if(Event.current.button == 1) {
    110.                     OnRightClick();
    111.                 }
    112.             }
    113.         }
    114.         else if (!available) {
    115.             GUI.Button(position, iconNotAvailable, "label");
    116.         }
    117.     }
    118.  
    119.     private bool AreRequiredMasteriesUnlocked() { //Checks if required masteried have points added to them
    120.         for (int i = 0; i < requiredMasteries.Count; i++) {
    121.             if (requiredMasteries[i++] != null) {
    122.                 if (requiredMasteries[i].PointsAdded == true) {
    123.                     continue;
    124.                 }
    125.                 else {
    126.                     return false;
    127.                 }
    128.             }
    129.             else if (requiredMasteries[i].PointsAdded == true) {
    130.                 return true;
    131.             }
    132.  
    133.             else {
    134.                 return false;
    135.             }
    136.         }
    137.  
    138.         Debug.Log("No Required Masteries");
    139.         return true;
    140.     }
    141.  
    142.     #region Getters & Setters
    143.    
    144.     public string MasteryName {
    145.         get { return masteryName; }
    146.         set { masteryName = value; }
    147.     }
    148.    
    149.     public string MasteryDescription {
    150.         get { return masteryDescription; }
    151.         set { masteryDescription = value; }
    152.     }
    153.    
    154.     public bool PointsAdded {
    155.         get { return pointsAdded; }
    156.         set { pointsAdded = value; }
    157.     }
    158.    
    159.     public bool Available {
    160.         get { return available; }
    161.         set { Available = value; }
    162.     }
    163.    
    164.     public int CurrentPointsAdded {
    165.         get { return currentPointsAdded; }
    166.         set { currentPointsAdded = value; }
    167.     }
    168.    
    169.     public int MaxPointsAdded {
    170.         get { return maxPointsAdded; }
    171.         set { maxPointsAdded = value; }
    172.     }
    173.    
    174.     public int RequiredLevel {
    175.         get { return requiredLevel; }
    176.         set { requiredLevel = value; }
    177.     }
    178.    
    179.     public Rect Position {
    180.         get { return position; }
    181.         set { position = value; }
    182.     }
    183.    
    184.     public Texture2D IconAvailable {
    185.         get { return iconAvailable; }
    186.         set { iconAvailable = value; }
    187.     }  
    188.    
    189.     public Texture2D IconNotAvailable {
    190.         get { return iconNotAvailable; }
    191.         set { iconNotAvailable = value; }
    192.     }
    193.    
    194.    
    195.    
    196.     #endregion
    197.  
    198.  
    199. }
     
  2. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    I'm not sure if this is what's causing the problems, but I imagine they are or will cause some:

    mastery.Start();
    mastery.Update();

    Both Start() and Update() are built in and run automatically which means whatever is in the Masteries script Start() gets run when the script first loads and everything in the Update() will get called every frame regardless of your foreach loop.
     
  3. kingbas33

    kingbas33

    Joined:
    Sep 9, 2014
    Posts:
    9
    I do not think that is true 'cause the script doesn't inherit from MonoBehaviour and isn't attached to a gameobject.
     
  4. kingbas33

    kingbas33

    Joined:
    Sep 9, 2014
    Posts:
    9
    Okay so i have found the problem: There is a Stack Overflow Exception thrown at the set from Available. I'm not really expierenced with c# so i do not know why this exception is thrown here.
    Anyone who could help me out?

    EDIT: Was looking at the code and found it: I spelled available = value with a capital A reffering it back to the property causing the stack overflow.
     
    Last edited: Sep 10, 2014
    jettyuen29 likes this.