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

Access and change variable

Discussion in 'Scripting' started by Afrokid001, Aug 25, 2013.

  1. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    Hi.

    I am making a Tic Tac Toe game, The issue I am having is I am wanting two players. 0 and 1. I have a "master script" that has the variable Player.

    I am wanting my boxes to know who's turn it is by asking the master script. The trouble is I cannot get them to interact with each other? What am I missing?

    I have 9 boxes arranged in 3 lines of 3.

    My scripts are:

    Code (csharp):
    1. static var Player = 0;
    Code (csharp):
    1.  
    2. var Cross : Material;
    3. var Naught : Material;
    4. var Blank : Material;
    5. var object : GameObject;
    6. var clicked = 0;
    7. var TTTBoard : GameObject;
    8. var Turn = TTTBoard.GetComponent(Board).Player;
    9.  
    10. function OnMouseEnter ()
    11. {
    12.     if (Turn == 1)
    13.     {
    14.         if (clicked == 0)
    15.         {
    16.             object.renderer.material = Cross;
    17.             GetComponent(MeshRenderer).enabled = true;
    18.         }
    19.         if (clicked == 1)
    20.         {
    21.         }
    22.     }
    23.     if (Turn == 0)
    24.     {
    25.         if (clicked == 0)
    26.         {
    27.             object.renderer.material = Naught;
    28.             GetComponent(MeshRenderer).enabled = true;
    29.         }
    30.         if (clicked == 1)
    31.         {
    32.         }
    33.     }
    34. }
    35.  
    36. function OnMouseExit ()
    37. {  
    38.     if (clicked == 0)
    39.     {
    40.         object.renderer.material = Blank;
    41.         GetComponent(MeshRenderer).enabled = false;
    42.     }
    43.     if (clicked == 1)
    44.     {
    45.     }
    46. }
    47.  
    48.  
    49. function OnMouseDown ()
    50. {
    51.     if (Turn == 1)
    52.     {
    53.         object.renderer.material = Cross;
    54.         GetComponent(MeshRenderer).enabled = true;
    55.         clicked = 1;
    56.         object.GetComponent( "ChangeBoxMaterial" ).enabled = false;
    57.         Turn = 0;
    58.     }
    59.     if (Turn == 0)
    60.     {
    61.         object.renderer.material = Naught;
    62.         GetComponent(MeshRenderer).enabled = true;
    63.         clicked = 1;
    64.         object.GetComponent( "ChangeBoxMaterial" ).enabled = false;
    65.         Turn = 1;
    66.     }
    67. }
     
  2. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    try making it public so other scripts are allowed to access it:
    Code (csharp):
    1. public static var Player = 0;
    you can then simply access this variable in other scripts with:
    Code (csharp):
    1. scriptname.variable = value;
    2. // for you it might be:
    3. masterscript.Player = 1;
     
  3. Afrokid001

    Afrokid001

    Joined:
    Jun 3, 2010
    Posts:
    89
    The material now changes depending on the player but when I click player = 1 instantly goes back to player = 0?


    Ok. I have realized that when I call the
    Code (csharp):
    1.  object.GetComponent( "ChangeBoxMaterial" ).enabled = false;
    it cancels the variable change. Is there a code I can use to make it stick when the code that changes it has been disabled?
     
    Last edited: Aug 25, 2013
  4. HelloKity1231

    HelloKity1231

    Joined:
    Nov 25, 2012
    Posts:
    121
    About Turn

    Create an enum with players and start a coroutine that will wait for action.
    On Update() function you can manage who has turn.

    A small example(maby is totally worng, i never tried to make something like this)
    TTTManager - http://pastebin.com/wFynSsZv
    TTTBox - http://pastebin.com/Y1Hdj2yf

    you can know who's turn by checking
    Code (csharp):
    1.  
    2. if (TTTManager.TTTTurn == TTTManager.Turn.???) { /* do */ }
    3.  
     
    Last edited: Aug 25, 2013