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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[CLOSED] Unidentifiable Crash when a script is enabled.

Discussion in 'Scripting' started by MewEight, Sep 17, 2015.

  1. MewEight

    MewEight

    Joined:
    Feb 28, 2014
    Posts:
    10
    When my WireLevelManager object is enabled, unity just instantly crashes.
    Urgently needs advice if possible.

    Sorry if this is wrong place.

    Edit 1: Problem comes mostlikely from LevelManager, just inheriting from it causes unity to crash
    Edit 2: Problem found to be not from Level Manager, instead it comes from my UIManager who listens to event from levelmanager
    Edit 3: Ok, this is weird to me now. I discovered the cause for the crash. It occurs when i call base.Start in any subclass of the LevelManager. Any idea? or is this common?
    Edit 4: Case closed. Lesson Learned : DONT RUSH STUFF!

    ** Solution Below **
     
    Last edited: Sep 17, 2015
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Ouch, you are doing some crazy bad stuff with properties here. Let me show you

    Code (CSharp):
    1. public int score
    2.     {
    3.         get{ return score;}
    4.         set
    5.         {
    6.             score = value;
    7.             if(value > 0)
    8.             {
    9.                 if(OnScoreIncreaseEvent != null)
    10.                 {
    11.                     OnScoreIncreaseEvent(score);
    12.                 }
    13.             }
    14.             else if(value < 0)
    15.             {
    16.                 if(OnScoreDecreaseEvent != null)
    17.                 {
    18.                     OnScoreDecreaseEvent(score);
    19.                 }
    20.             }
    21.         }
    22.     }
    When you set score anywhere in code (like you do in your start method) you will cause an endless recursive loop.

    score = 10;

    this will call the set method of score, which sets the score property, which will call the set method of score, which will set the score property which will call the set property of score.

    Get the idea?

    You should never, or rarely, set the property itself in its own set method unless you know there is a way for it to be recurse forever. Even then its evil.

    So what is happening is, unity is starting your game, runs the Start method:

    Code (CSharp):
    1. protected virtual void Start()
    2.     {
    3.         lives = 5;
    4.         timeRemaining = 50.0f;
    5.         score = 50;
    6.         gameState = LevelState.PRE_GAME;
    7.     }
    hits the lives = 5 (which has the same issue as score) line and dies in a recursive death fireball and as Unity shares the same thread as the game, your game takes unity with it.

    To fix it, you need to have a private variable behind score:

    Code (CSharp):
    1. private int mScore;
    2. public int score
    3.     {
    4.         get{ return mScore;}
    5.         set
    6.         {
    7.             mScore = value;
    8.             if(value > 0)
    9.             {
    10.                 if(OnScoreIncreaseEvent != null)
    11.                 {
    12.                     OnScoreIncreaseEvent(mScore);
    13.                 }
    14.             }
    15.             else if(value < 0)
    16.             {
    17.                 if(OnScoreDecreaseEvent != null)
    18.                 {
    19.                     OnScoreDecreaseEvent(mScore);
    20.                 }
    21.             }
    22.         }
    23.     }
     
    MewEight likes this.
  3. MewEight

    MewEight

    Joined:
    Feb 28, 2014
    Posts:
    10
    :oops: was on a rush.. i usually do a private and a public getter and setter. Thanks! will try it
     
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Don't worry. Happens to everyone. Good luck!