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

Help me please.

Discussion in 'Scripting' started by ThomasMartin, Oct 29, 2014.

  1. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    I have a problem. I am changing the state of a bool in another script but I don't know how to make something happen in my other script. I can't use Void Update because that happens every frame and I can't use Void Start because that happens when I begin the game. Is there any other way I could do this.

    P.S sorry for the description but I don't really know how to explain the problem. Also sorry for the title. I do not know what I should of used.
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Use a property for your bool like so :


    Code (CSharp):
    1. public bool MyBool
    2.     {
    3.         get { return _mybool; }
    4.         set
    5.         {
    6.                 _mybool = value;
    7.                 //call a method or do something else
    8.         }
    9.     }
    10.     private bool _mybool;
    This way whenever another script sets the value of MyBool, you can execute whatever logic you need. You could also add a check to validate that the new value is different from the current value before executing your logic.
     
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Tut tut... use descriptive topic titles ;)
     
  4. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    You sir, are amazing. Thanks!
     
  5. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    I tried. I honestly could not think of a title. Hell, I could just about describe the problem I had.