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

setting boolean in one script from another

Discussion in 'Scripting' started by ThomasUnity, Nov 16, 2015.

  1. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    I want to set a boolean from true in it's own script, to false from another. But it doesn't work. I don't get any errors, but the boolean stays set to true.
    It seems such a simple issue, but it ruined my afternoon... Anybody sees where the problem is?

    The one script, called "gameover", attached to a GameObject called "Watcher":
    Code (csharp):
    1.  
    2. #pragma strict
    3. var gameover: boolean;
    4.  
    5. function Awake () {
    6. gameover = true;
    7. }
    8.  
    and the other script , called "setgame':

    Code (csharp):
    1.  
    2. #pragma strict
    3. private var game:boolean;
    4.  
    5. function Awake () {
    6.     game = GameObject.Find("Watcher").GetComponent.<Gameover>().gameover;
    7. }
    8.  
    9. function Start () {
    10.     game = false;
    11. }
    12.  
     
    Last edited: Nov 16, 2015
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Use [code ]code tags[/code] to make your code look better and make it easier for us to read it and help you.

    You never set Gameover.gameover to anything. When you affect "game", it only affects your local copy of the bool.
     
  3. ThomasUnity

    ThomasUnity

    Joined:
    Jul 10, 2015
    Posts:
    17
    You helped me again! This time, i have made a variable for the Gameover component in the other gameobject and then after a dot, call the real boolean or integer! Thank you, StarManta!

    Code (csharp):
    1.  
    2. #pragma strict
    3. private var game:Gameover;
    4.  
    5. function Awake () {
    6. game = GameObject.Find("Watcher").GetComponent.<Gameover>();
    7. }
    8.  
    9. function Start () {
    10. game.gameover = true;
    11. game.howmuch = 78;
    12. }
    13.