Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need script to read results of dice roll from another script.

Discussion in 'Scripting' started by rallyall, Jan 16, 2020.

  1. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    I'm new to all this. I'm having a hard time figuring out how to make scripts talk to each other. I copy what I see in tutorials but it doesn't work. I'm still at that stage where I can't adapt what I've seen to my specific case. Unless it's exact I keep getting lost. Here's the specific application I'm looking for.

    I want my GameController script (which I copied from a tic-tac-toe tutorial) to be able to see the finalSide result from my ZoneDice script and do something with it. This something is on line 200 of GameController, I want to remove the toggle "command" (or whatever it's technically called) and replace it with the finalSide result. Which is meant to determine what buttons in the buttonList get activated, ie interactable.

    Thanks for the help.

    GameController script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. [System.Serializable]
    8. public class Player
    9. {
    10.     public Image panel;
    11.     public Text text;
    12.     public Button button;
    13.    
    14.  
    15. }
    16.  
    17.  
    18. [System.Serializable]
    19. public class PlayerColor
    20. {
    21.     public Color panelColor;
    22.     public Color textColor;
    23. }
    24.  
    25.  
    26.  
    27. public class GameController : MonoBehaviour
    28. {
    29.    
    30.  
    31.     public Text[] buttonList;
    32.     public GameObject gameOverPanel;
    33.     public Text gameOverText;
    34.     public GameObject restartButton;
    35.     public GameObject pieceButton;
    36.     public Player playerX;
    37.     public Player playerO;
    38.     public PlayerColor activePlayerColor;
    39.     public PlayerColor inactivePlayerColor;
    40.     public GameObject startInfo;
    41.  
    42.     private string playerSide;
    43.     private int moveCount;
    44.  
    45.    
    46.     void Awake()
    47.     {
    48.         SetGameControllerReferenceOnButtons();
    49.         gameOverPanel.SetActive(false);
    50.         moveCount = 0;
    51.         restartButton.SetActive(false);
    52.         pieceButton.SetActive(false);
    53.     }
    54.  
    55.    void SetGameControllerReferenceOnButtons()
    56.     { //loops
    57.        
    58.         for (int i = 0; i < buttonList.Length; i++)
    59.         {
    60.             buttonList[i].GetComponentInParent<GridSpace>().SetGameControllerReference(this);
    61.         }
    62.     }
    63.  
    64.     public void SetStartingSide(string startingSide)
    65.     {
    66.         playerSide = startingSide;
    67.         if (playerSide == "X")
    68.         {
    69.             SetPlayerColors(playerX, playerO);
    70.         }
    71.         else
    72.         {
    73.             SetPlayerColors(playerO, playerX);
    74.         }
    75.  
    76.         StartGame();
    77.     }
    78.  
    79.     void StartGame()
    80.     {
    81.         SetBoardInteractable(true);
    82.         SetPlayerButtons(false);
    83.         startInfo.SetActive(false);
    84.     }
    85.  
    86.     public string GetPlayerSide()
    87.     {
    88.         return playerSide;
    89.     }
    90.  
    91.     public void EndTurn()
    92.     {
    93.         moveCount++;
    94.  
    95.         if (buttonList[0].text == playerSide && buttonList[1].text == playerSide && buttonList[2].text == playerSide)
    96.         {
    97.             GameOver(playerSide);
    98.         }
    99.         else if (buttonList[3].text == playerSide && buttonList[4].text == playerSide && buttonList[5].text == playerSide)
    100.         {
    101.             GameOver(playerSide);
    102.         }
    103.         else if (buttonList[6].text == playerSide && buttonList[7].text == playerSide && buttonList[8].text == playerSide)
    104.         {
    105.             GameOver(playerSide);
    106.         }
    107.         else if (buttonList[0].text == playerSide && buttonList[3].text == playerSide && buttonList[6].text == playerSide)
    108.         {
    109.             GameOver(playerSide);
    110.         }
    111.         else if (buttonList[1].text == playerSide && buttonList[4].text == playerSide && buttonList[7].text == playerSide)
    112.         {
    113.             GameOver(playerSide);
    114.         }
    115.         else if (buttonList[2].text == playerSide && buttonList[5].text == playerSide && buttonList[8].text == playerSide)
    116.         {
    117.             GameOver(playerSide);
    118.         }
    119.         else if (buttonList[0].text == playerSide && buttonList[4].text == playerSide && buttonList[8].text == playerSide)
    120.         {
    121.             GameOver(playerSide);
    122.         }
    123.         else if (buttonList[2].text == playerSide && buttonList[4].text == playerSide && buttonList[6].text == playerSide)
    124.         {
    125.             GameOver(playerSide);
    126.         }
    127.         else if (moveCount >= 9)
    128.         {
    129.             GameOver("draw");
    130.         }
    131.         else
    132.         {
    133.             ChangeSides();
    134.         }
    135.     }
    136.  
    137.     void ChangeSides()
    138.  
    139.         //Ternary
    140.     {
    141.         playerSide = (playerSide == "X") ? "O" : "X";
    142.         if (playerSide == "X")
    143.         {
    144.             SetPlayerColors(playerX, playerO);
    145.         }
    146.         else
    147.         {
    148.             SetPlayerColors(playerO, playerX);
    149.         }
    150.     }
    151.  
    152.     void SetPlayerColors(Player newPlayer, Player oldPlayer)
    153.     {
    154.         newPlayer.panel.color = activePlayerColor.panelColor;
    155.         newPlayer.text.color = activePlayerColor.textColor;
    156.         oldPlayer.panel.color = inactivePlayerColor.panelColor;
    157.         oldPlayer.text.color = inactivePlayerColor.textColor;
    158.     }
    159.  
    160.     void GameOver(string winningPlayer)
    161.     { //conditionals
    162.         SetBoardInteractable(false);
    163.         if (winningPlayer == "draw")
    164.         {
    165.             SetGameOverText("It's a Draw!");
    166.             SetPlayerColorsInactive();
    167.         }
    168.         else
    169.         {
    170.             SetGameOverText(winningPlayer + " Wins!");
    171.         }
    172.         restartButton.SetActive(true);
    173.     }
    174.  
    175.     void SetGameOverText(string value)
    176.     {
    177.         gameOverPanel.SetActive(true);
    178.         gameOverText.text = value;
    179.     }
    180.  
    181.     public void RestartGame()
    182.     {
    183.         moveCount = 0;
    184.         gameOverPanel.SetActive(false);
    185.         restartButton.SetActive(false);
    186.         SetPlayerButtons(true);
    187.         SetPlayerColorsInactive();
    188.         startInfo.SetActive(true);
    189.  
    190.         for (int i = 0; i < buttonList.Length; i++)
    191.         {
    192.             buttonList[i].text = "";
    193.         }
    194.     }
    195.  
    196.    void SetBoardInteractable(bool toggle)
    197.     {//this code makes grid space active, replace toggle with dice roll?
    198.  
    199.         for (int i = 0; i < buttonList.Length; i++)
    200.         {
    201.             buttonList[i].GetComponentInParent<Button>().interactable = toggle;
    202.         }
    203.     }
    204.  
    205.     void SetPlayerButtons(bool toggle)
    206.     {
    207.         playerX.button.interactable = toggle;
    208.        playerO.button.interactable = toggle;
    209.     }
    210.  
    211.     void SetPlayerColorsInactive()
    212.     {
    213.         playerX.panel.color = inactivePlayerColor.panelColor;
    214.         playerX.text.color = inactivePlayerColor.textColor;
    215.         playerO.panel.color = inactivePlayerColor.panelColor;
    216.         playerO.text.color = inactivePlayerColor.textColor;
    217.     }
    218. }
    219.  
    ZoneDice script:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6.  
    7. public class ZoneDice : MonoBehaviour {
    8.  
    9.     // Array of dice sides sprites to load from Resources folder
    10.     private Sprite[] diceSides;
    11.  
    12.     // Reference to sprite renderer to change sprites
    13.     private SpriteRenderer rend;
    14.  
    15.     // Use this for initialization
    16.     private void Start () {
    17.  
    18.         // Assign Renderer component
    19.         rend = GetComponent<SpriteRenderer>();
    20.  
    21.         // Load dice sides sprites to array from DiceSides subfolder of Resources folder
    22.         diceSides = Resources.LoadAll<Sprite>("ZoneSides/");
    23.     }
    24.    
    25.     // If you left click over the dice then RollTheDice coroutine is started
    26.     private void OnMouseDown()
    27.     {
    28.         StartCoroutine("RollTheDice");
    29.     }
    30.  
    31.     // Coroutine that rolls the dice
    32.     public IEnumerator RollTheDice()
    33.     {
    34.         // Variable to contain random dice side number.
    35.         // It needs to be assigned. Let it be 0 initially
    36.         int randomDiceSide = 6;
    37.  
    38.         // Final side or value that dice reads in the end of coroutine
    39.         int finalSide = 0;
    40.  
    41.         // Loop to switch dice sides ramdomly
    42.         // before final side appears. 20 itterations here.
    43.         for (int i = 0; i <= 20; i++)
    44.         {
    45.             // Pick up random value from 0 to 5 (All inclusive)
    46.             randomDiceSide = Random.Range(0, 6);
    47.  
    48.             // Set sprite to upper face of dice from array according to random value
    49.             rend.sprite = diceSides[randomDiceSide];
    50.  
    51.             // Pause before next itteration
    52.             yield return new WaitForSeconds(0.05f);
    53.         }
    54.  
    55.         // Assigning final side so you can use this value later in your game
    56.         // for player movement for example
    57.         finalSide = randomDiceSide + 1;
    58.  
    59.        
    60.  
    61.         if (finalSide == 1)
    62.         {
    63.             Debug.Log(finalSide + " Place in shark zone");
    64.         }
    65.         if (finalSide == 2)
    66.         {
    67.             Debug.Log(finalSide + " Place in zone 2");
    68.         }
    69.         if (finalSide == 3)
    70.         {
    71.             Debug.Log(finalSide + " Place in zone 3");
    72.         }
    73.         if (finalSide == 4)
    74.         {
    75.             Debug.Log(finalSide + " Place in zone 4");
    76.         }
    77.         if (finalSide == 5)
    78.         {
    79.             Debug.Log(finalSide + " Place in zone 5");
    80.         }
    81.         if (finalSide == 6)
    82.         {
    83.             Debug.Log(finalSide + " Place in shark zone");
    84.         }
    85.     }
    86. }
    87.  
    88.  
     
  2. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    you can make finalSide a static variable, but this isn't advisable.

    you can make it public and then put a reference to your ZoneDice script in your GameController script and then you'll have access to that script's variables.

    i'd declare finalSide outside the scope of that particular method, or make another variable outside that scope that references it first. so line 11 of your zone dice script put

    Code (CSharp):
    1. public int finalSide;
    and replace your line 39.

    then in your game controller declare the following variables globally, so just put them on line 12 and 13.

    Code (CSharp):
    1. public GameObject zoneDiceScriptObject;
    2. public ZoneDice zoneDiceScript;
    then in start put

    Code (CSharp):
    1. zoneDiceScript = zoneDiceScriptObject.GetComponent<ZoneDice>();

    Put your ZoneDice script on an object, which you assign to the zoneDiceScriptObject, you can just drag it in if you leave it public like this.

    now you can reference your ZoneDice finalSide variable from your GameController script like this.

    zoneDiceScript.finalSide
     
    rallyall likes this.
  3. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Thanks for the help. I'm getting errors though. CS0103, it's saying zoneDiceScript and zoneDiceScriptObject do not exist in the current context.

    My GameController script doesn't have a start method so I created one. Here's how it looks.

    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         zoneDiceScript = zoneDiceScriptObject.GetComponent<ZoneDice>();
    5.     }
    6.  
    and how the code looks where I'm getting the errors:

    Code (csharp):
    1.  
    2. public class Player
    3. {
    4.     public Image panel;
    5.     public Text text;
    6.     public Button button;
    7.     public GameObject zoneDiceScriptObject;
    8.     public ZoneDice zoneDiceScript;
    9. }
    10.  
    11.  
    And I'll still need help getting the finalSide of the public variable to be usable. Can I simply do Debug.Log(finalSide); inside my GameController script to verify that it is reading it?
     
    Last edited: Jan 16, 2020
  4. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    you can just use debug to verify that it's reading it. Debug.Log ("finalSide equals " + zoneDiceScript.finalSide); did you remember to drag the actual object that you have your zoneDiceScript on into the zoneDiceScriptObject field in your inspector?
     
  5. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Well the inspector won't update with the field until the errors are gone. I can get all the errors to be fixed if I remove , public GameObject zoneDiceScriptObject;. Then I dragged the object into the zoneDiceScript field. No errors. But I'm not finding the finalSide variable showing up yet. I'll try your Debug suggestion.
     
  6. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Yeah I can't get it to work or verify that the GameController script is even seeing the finalSide variable.

    This is what I put in the start method of the GameController script. What's been commented out are things I have tried but they don't work.
    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         /* zoneDiceScript.finalSide();*/
    5.  
    6.         /*zoneDiceScript = zoneDiceScript.GetComponent<ZoneDice>();
    7.  
    8.             int dice = zoneDiceScript.GetComponent<ZoneDice>().finalSide;*/
    9.         /* Debug.Log("finalSide equals " + zoneDiceScriptObject.finalSide);*/
    10.  
    11.         zoneDiceScript = zoneDiceScriptObject.GetComponent<ZoneDice>();
    12.          Debug.Log("finalSide equals " + zoneDiceScript.finalSide);
    13.    
    14.     }
    15.  
    And when I include
    Debug.Log("finalSide equals " + zoneDiceScriptObject.finalSide);
    I get warning message
    Assets\Scripts\GameController.cs(50,62): error CS1061: 'GameObject' does not contain a definition for 'finalSide' and no accessible extension method 'finalSide' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
     
  7. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    sorry, i spaced out telling you to add those two lines to lines 12 and 13, they should go in the scope of your gamecontroller. add a "ZoneDice" tag to the object you put your zone dice script on and then replace your codes with these.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. [System.Serializable]
    7. public class Player
    8. {
    9.     public Image panel;
    10.     public Text text;
    11.     public Button button;
    12.  
    13.  
    14. }
    15.  
    16.  
    17. [System.Serializable]
    18. public class PlayerColor
    19. {
    20.     public Color panelColor;
    21.     public Color textColor;
    22. }
    23.  
    24.  
    25.  
    26. public class GameController : MonoBehaviour
    27. {
    28.  
    29.  
    30.     public Text[] buttonList;
    31.     public GameObject gameOverPanel;
    32.     public Text gameOverText;
    33.     public GameObject restartButton;
    34.     public GameObject pieceButton;
    35.     public Player playerX;
    36.     public Player playerO;
    37.     public PlayerColor activePlayerColor;
    38.     public PlayerColor inactivePlayerColor;
    39.     public GameObject startInfo;
    40.     public GameObject zoneDiceScriptObject;
    41.     public ZoneDice zoneDiceScript;
    42.  
    43.     private string playerSide;
    44.     private int moveCount;
    45.  
    46.  
    47.     void Awake()
    48.     {
    49.         SetGameControllerReferenceOnButtons();
    50.         gameOverPanel.SetActive(false);
    51.         moveCount = 0;
    52.         restartButton.SetActive(false);
    53.         pieceButton.SetActive(false);
    54.     zoneDiceScriptObject = GameObject.FindWithTag ("ZoneDice");
    55.         zoneDiceScript = zoneDiceScriptObject.GetComponent<ZoneDice>();
    56.     }
    57.  
    58.    void SetGameControllerReferenceOnButtons()
    59.     { //loops
    60.    
    61.         for (int i = 0; i < buttonList.Length; i++)
    62.         {
    63.             buttonList[i].GetComponentInParent<GridSpace>().SetGameControllerReference(this);
    64.         }
    65.     }
    66.  
    67.     public void SetStartingSide(string startingSide)
    68.     {
    69.         playerSide = startingSide;
    70.         if (playerSide == "X")
    71.         {
    72.             SetPlayerColors(playerX, playerO);
    73.         }
    74.         else
    75.         {
    76.             SetPlayerColors(playerO, playerX);
    77.         }
    78.  
    79.         StartGame();
    80.     }
    81.  
    82.     void StartGame()
    83.     {
    84.         SetBoardInteractable(true);
    85.         SetPlayerButtons(false);
    86.         startInfo.SetActive(false);
    87.     }
    88.  
    89.     public string GetPlayerSide()
    90.     {
    91.         return playerSide;
    92.     }
    93.  
    94.     public void EndTurn()
    95.     {
    96.         moveCount++;
    97.  
    98.         if (buttonList[0].text == playerSide && buttonList[1].text == playerSide && buttonList[2].text == playerSide)
    99.         {
    100.             GameOver(playerSide);
    101.         }
    102.         else if (buttonList[3].text == playerSide && buttonList[4].text == playerSide && buttonList[5].text == playerSide)
    103.         {
    104.             GameOver(playerSide);
    105.         }
    106.         else if (buttonList[6].text == playerSide && buttonList[7].text == playerSide && buttonList[8].text == playerSide)
    107.         {
    108.             GameOver(playerSide);
    109.         }
    110.         else if (buttonList[0].text == playerSide && buttonList[3].text == playerSide && buttonList[6].text == playerSide)
    111.         {
    112.             GameOver(playerSide);
    113.         }
    114.         else if (buttonList[1].text == playerSide && buttonList[4].text == playerSide && buttonList[7].text == playerSide)
    115.         {
    116.             GameOver(playerSide);
    117.         }
    118.         else if (buttonList[2].text == playerSide && buttonList[5].text == playerSide && buttonList[8].text == playerSide)
    119.         {
    120.             GameOver(playerSide);
    121.         }
    122.         else if (buttonList[0].text == playerSide && buttonList[4].text == playerSide && buttonList[8].text == playerSide)
    123.         {
    124.             GameOver(playerSide);
    125.         }
    126.         else if (buttonList[2].text == playerSide && buttonList[4].text == playerSide && buttonList[6].text == playerSide)
    127.         {
    128.             GameOver(playerSide);
    129.         }
    130.         else if (moveCount >= 9)
    131.         {
    132.             GameOver("draw");
    133.         }
    134.         else
    135.         {
    136.             ChangeSides();
    137.         }
    138.     }
    139.  
    140.     void ChangeSides()
    141.  
    142.         //Ternary
    143.     {
    144.         playerSide = (playerSide == "X") ? "O" : "X";
    145.         if (playerSide == "X")
    146.         {
    147.             SetPlayerColors(playerX, playerO);
    148.         }
    149.         else
    150.         {
    151.             SetPlayerColors(playerO, playerX);
    152.         }
    153.     }
    154.  
    155.     void SetPlayerColors(Player newPlayer, Player oldPlayer)
    156.     {
    157.         newPlayer.panel.color = activePlayerColor.panelColor;
    158.         newPlayer.text.color = activePlayerColor.textColor;
    159.         oldPlayer.panel.color = inactivePlayerColor.panelColor;
    160.         oldPlayer.text.color = inactivePlayerColor.textColor;
    161.     }
    162.  
    163.     void GameOver(string winningPlayer)
    164.     { //conditionals
    165.         SetBoardInteractable(false);
    166.         if (winningPlayer == "draw")
    167.         {
    168.             SetGameOverText("It's a Draw!");
    169.             SetPlayerColorsInactive();
    170.         }
    171.         else
    172.         {
    173.             SetGameOverText(winningPlayer + " Wins!");
    174.         }
    175.         restartButton.SetActive(true);
    176.     }
    177.  
    178.     void SetGameOverText(string value)
    179.     {
    180.         gameOverPanel.SetActive(true);
    181.         gameOverText.text = value;
    182.     }
    183.  
    184.     public void RestartGame()
    185.     {
    186.         moveCount = 0;
    187.         gameOverPanel.SetActive(false);
    188.         restartButton.SetActive(false);
    189.         SetPlayerButtons(true);
    190.         SetPlayerColorsInactive();
    191.         startInfo.SetActive(true);
    192.  
    193.         for (int i = 0; i < buttonList.Length; i++)
    194.         {
    195.             buttonList[i].text = "";
    196.         }
    197.     }
    198.  
    199.    void SetBoardInteractable(bool toggle)
    200.     {//this code makes grid space active, replace toggle with dice roll?
    201.  
    202.         for (int i = 0; i < buttonList.Length; i++)
    203.         {
    204.             buttonList[i].GetComponentInParent<Button>().interactable = toggle;
    205.         }
    206.     }
    207.  
    208.     void SetPlayerButtons(bool toggle)
    209.     {
    210.         playerX.button.interactable = toggle;
    211.        playerO.button.interactable = toggle;
    212.     }
    213.  
    214.     void SetPlayerColorsInactive()
    215.     {
    216.         playerX.panel.color = inactivePlayerColor.panelColor;
    217.         playerX.text.color = inactivePlayerColor.textColor;
    218.         playerO.panel.color = inactivePlayerColor.panelColor;
    219.         playerO.text.color = inactivePlayerColor.textColor;
    220.     }
    221. }
    Code (csharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6. public class ZoneDice : MonoBehaviour {
    7.  
    8.     // Array of dice sides sprites to load from Resources folder
    9.     private Sprite[] diceSides;
    10.     public int finalSide;
    11.  
    12.     // Reference to sprite renderer to change sprites
    13.     private SpriteRenderer rend;
    14.  
    15.     // Use this for initialization
    16.     private void Start () {
    17.  
    18.         // Assign Renderer component
    19.         rend = GetComponent<SpriteRenderer>();
    20.  
    21.         // Load dice sides sprites to array from DiceSides subfolder of Resources folder
    22.         diceSides = Resources.LoadAll<Sprite>("ZoneSides/");
    23.     }
    24.  
    25.     // If you left click over the dice then RollTheDice coroutine is started
    26.     private void OnMouseDown()
    27.     {
    28.         StartCoroutine("RollTheDice");
    29.     }
    30.  
    31.     // Coroutine that rolls the dice
    32.     public IEnumerator RollTheDice()
    33.     {
    34.         // Variable to contain random dice side number.
    35.         // It needs to be assigned. Let it be 0 initially
    36.         int randomDiceSide = 6;
    37.  
    38.         // Final side or value that dice reads in the end of coroutine
    39.         finalSide = 0;
    40.  
    41.         // Loop to switch dice sides ramdomly
    42.         // before final side appears. 20 itterations here.
    43.         for (int i = 0; i <= 20; i++)
    44.         {
    45.             // Pick up random value from 0 to 5 (All inclusive)
    46.             randomDiceSide = Random.Range(0, 6);
    47.  
    48.             // Set sprite to upper face of dice from array according to random value
    49.             rend.sprite = diceSides[randomDiceSide];
    50.  
    51.             // Pause before next itteration
    52.             yield return new WaitForSeconds(0.05f);
    53.         }
    54.  
    55.         // Assigning final side so you can use this value later in your game
    56.         // for player movement for example
    57.         finalSide = randomDiceSide + 1;
    58.  
    59.    
    60.  
    61.         if (finalSide == 1)
    62.         {
    63.             Debug.Log(finalSide + " Place in shark zone");
    64.         }
    65.         if (finalSide == 2)
    66.         {
    67.             Debug.Log(finalSide + " Place in zone 2");
    68.         }
    69.         if (finalSide == 3)
    70.         {
    71.             Debug.Log(finalSide + " Place in zone 3");
    72.         }
    73.         if (finalSide == 4)
    74.         {
    75.             Debug.Log(finalSide + " Place in zone 4");
    76.         }
    77.         if (finalSide == 5)
    78.         {
    79.             Debug.Log(finalSide + " Place in zone 5");
    80.         }
    81.         if (finalSide == 6)
    82.         {
    83.             Debug.Log(finalSide + " Place in shark zone");
    84.         }
    85.     }
    86. }
    87.  
    and then access it as i said before with zoneDiceScript.finalSide.

    by the way, this is just how you access variables from script to script. i don't know exactly what you're trying to do with this toggle. your finalSide is going to be an int value, which is just whole numbers. your toggle seems to be a bool. i'm not exactly sure where you're going with that.

    the method at line 200 is a loop that goes through all of the spaces on your board and activates or deactivates them, it won't do anything with your dice value.
     
    Last edited: Jan 17, 2020
  8. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Okay I did all those things and the scene plays. But I can't get the finalSide to show any results. I've put Debug.Log("GC can see " + zoneDiceScript.finalSide); all over the place in the GameController (GC) script. It's in void start, it's in void awake, it's in void startgame, I even tried making a void update and it didn't like it. Tried public int start, didn't like it.... I know GameController can see into ZoneDice though because when I change the spelling of finalSide in either script I get an error. So I'm confident it's seeing it. I just can't find a way to get GameController to print the results.

    Thanks for your help thus far.

    Yeah I'm hoping there's a way to replace the bool toggle with the dice results. Something like, if 1 is rolled activate spaces 1-10, if 2 is rolled activate spaces 11-20.... But one thing at a time. There are plenty of tutorials on how to do dice rolling but almost none on how to make stuff happen as a result of the dice result.
     
  9. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    i think you'd be better off building something from the ground up once you get a feeling for the basics. I'm not sure what your goal is, but you'll learn a lot more a lot quicker if you follow some basic C# tutorials on scope, variables, and methods.

    if you just want to confirm that it's understanding the value, try declaring another global public int and either make it public or serialize it, then set it equal to finalSide in update. so line 45 on your GameController "public int diceRoll;" then "diceRoll = zoneDiceScript.finalSide;" in an update method in your GameController class.

    the value of diceRoll will be the same as finalSide and update accordingly.

    try changing line 10 to "public int finalSide = 0;" it shouldn't make a difference.
     
  10. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Yeah I have been following tutorials. They are minimally helpful. Thanks for your help.

    After doing all that I still can't find a place where Debug.Log will work. Niether of these,
    Debug.Log("GC can see " + diceRoll);
    Debug.Log("GC can see " + zoneDiceScript.finalSide);
    Are working in my GameController script.

    I added it to the void update I created. Probably doesn't work there because it's void. Making the update public seems to turn it into a class where it's asking for a return.

    Right now all I'm asking is where does the Debug.Log go in the GameController script where the finalSide result should appear?
     
    Last edited: Jan 20, 2020
  11. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    215
    It doesn't look like your dice rolling code makes any calls to the GameController to let it know a dice roll occurred. You should still be able for debugging purposes to put it in your GameController.Update() function. It'll spam the console but it should show when the dice roll occurs
     
  12. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    215
    Unity calls SOME functions automagically, like Update(), but if you wrote your own and called it update(), then that doesn't exactly match the signature (lowercase vs uppercase) so it wouldn't be called automatically, thats probably why it didn't work. I'd say try again but make sure its capitalized
     
  13. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    your debug.log would go in Update as vestigial stated. usually there would be a condition it would occur under. you could put a public bool in your roll the dice method, and have your debug only occur if that bool is true, and then set it to false.

    i'd definitely advise to just continue working with various basic tutorials before trying to create your own game. keep your goals simple at first, and keep achieving them, and something like this will be much simpler.

    perhaps this video will better demonstrate the concept.
     
  14. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    I'm getting close with your guys help. I did follow that YouTube video. I'm not sure what to do with the bool part nor the public void move stuff. Is that what I need to make the following issue work?

    When I play my scene and put different numbers in the inspector window for my dice script here:

    Code (csharp):
    1. public int finalZoneSide;
    I do get the changed number to show up in my GameController (GC) script here:

    Code (csharp):
    1. Debug.Log("GC in void Update can see zone side " + zoneDiceScript.finalZoneSide);
    However, when I click the dice button for how the players will change the number it does show up in my dice script here:...

    Code (csharp):
    1.  if (finalZoneSide == 2)
    2.         {
    3.             Debug.Log("Place in zone 2");
    ...but does not show up in my GameController script. Any ideas on how to get the receiving script (GC) to see when the button is pressed and then read the results?
     
  15. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Anyone? I'm so close. I can't figure out why my receiving script sees the changes to the public inspector window but it doesn't see the button click results for the dice roll of the same int name.
     
  16. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Okay I think I got it boiled down to this. Even the dice rolling script itself doesn't see the dice roll except for within the public IEnumerator RollTheDice() function. I verified this with a void update function looking for the finalZoneSide variable that's outside the RollTheDice function. It only sees the number that is in the public field in the inspector. It doesn't see the finalZoneSide that is within the RollTheDice function. So... how do I get the script itself and more importantly a different script to see into the RollTheDice function and see the roll results?

    I tried a tutorial and did this within the receiving script (GC)
    Code (CSharp):
    1.  
    2. private ZoneDice myZoneDice;
    3.  
    4.  void Start()
    5.     {
    6.      
    7.         myZoneDice = new ZoneDice();
    8.         myZoneDice.RollTheDice(myZoneDice.finalZoneSide);
    9.  
    10.     }
    11.  
    But unity gets mad and says no overload for method 'RollTheDice' takes 1 arguments (CS1501). I don't know what that means or how to fix it.

    Here's the current dice script:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7.  
    8. public class ZoneDice : MonoBehaviour {
    9.  
    10.  
    11.     // Array of dice sides sprites to load from Resources folder
    12.     private Sprite[] diceSides;
    13.  
    14.     public int finalZoneSide;
    15.  
    16.    // Reference to sprite renderer to change sprites
    17.     private SpriteRenderer rend;
    18.  
    19.   /*  public int diceResult()
    20.     {
    21.         return finalSide;
    22.     }*/
    23.  
    24.  
    25.     // Use this for initialization
    26.     private void Start () {
    27.  
    28.  
    29.         // Assign Renderer component
    30.         rend = GetComponent<SpriteRenderer>();
    31.  
    32.         // Load dice sides sprites to array from DiceSides subfolder of Resources folder
    33.         diceSides = Resources.LoadAll<Sprite>("ZoneSides/");
    34.  
    35.         Debug.Log("ZoneDice in private void Start " + finalZoneSide);
    36.    
    37.     }
    38.  
    39.    // If you left click over the dice then RollTheDice coroutine is started
    40.     private void OnMouseDown()
    41.     {
    42.         StartCoroutine("RollTheDice");
    43.     }
    44.  
    45.     // Coroutine that rolls the dice
    46.    public IEnumerator RollTheDice()
    47.     {
    48.         // Variable to contain random dice side number.
    49.         // It needs to be assigned. Let it be 0 initially
    50.         int randomDiceSide = 0;
    51.  
    52.         // Final side or value that dice reads in the end of coroutine
    53.        int finalZoneSide = 0;
    54.    
    55.  
    56.         // Loop to switch dice sides ramdomly
    57.         // before final side appears. 20 itterations here.
    58.         for (int i = 0; i <= 20; i++)
    59.         {
    60.             // Pick up random value from 0 to 5 (All inclusive)
    61.             randomDiceSide = Random.Range(0, 6);
    62.  
    63.             // Set sprite to upper face of dice from array according to random value
    64.             rend.sprite = diceSides[randomDiceSide];
    65.  
    66.             // Pause before next itteration
    67.             yield return new WaitForSeconds(0.05f);
    68.         }
    69.  
    70.         // Assigning final side so you can use this value later in your game
    71.         // for player movement for example
    72.         finalZoneSide = randomDiceSide + 1;
    73.    
    74.  
    75.         // Show final dice value in Console
    76.         /*Debug.Log(finalSide);*/
    77.  
    78.         if (finalZoneSide == 1)
    79.         {
    80.             Debug.Log("From ZD place in shark zone");
    81.         }
    82.         if (finalZoneSide == 2)
    83.         {
    84.             Debug.Log("From ZD place in zone 2");
    85.         }
    86.         if (finalZoneSide == 3)
    87.         {
    88.             Debug.Log("From ZD place in zone 3");
    89.         }
    90.         if (finalZoneSide == 4)
    91.         {
    92.             Debug.Log("From ZD place in zone 4");
    93.         }
    94.         if (finalZoneSide == 5)
    95.         {
    96.             Debug.Log("From ZD place in zone 5");
    97.         }
    98.         if (finalZoneSide == 6)
    99.         {
    100.             Debug.Log("From ZD place in shark zone");
    101.         }
    102.    
    103.     }
    104. }
    105.  
    106.  
     
  17. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    No one knows how to do this?
     
  18. RaceGT

    RaceGT

    Joined:
    Aug 3, 2017
    Posts:
    4
    Having quickly examined your latest post, when you're calling

    Code (CSharp):
    1. myZoneDice.RollTheDice(myZoneDice.finalZoneSide);
    you're passing an (supposed) argument into the function. But, as we see in the function declaration itself:

    Code (CSharp):
    1. public IEnumerator RollTheDice()
    that function takes no incoming arguments ("parameters"?) because the "()" is empty. That's why Unity is saying "hey, this function doesn't take any incoming parameters, but they're trying to pass one in, and I don't know what to do with what they're trying pass in". If it did, it would look something like:

    Code (CSharp):
    1. int public IEnumerator RollTheDice(int how_many_times_to_roll)
    but it doesn't.

    The "int" at the start of the function (method) says what type of variable this method will return when it's done, and this seems like what it needs: a
    return
    for when that function has been called and finished, something like:

    Code (CSharp):
    1. int result_of_dice_roll = whatever;
    2. return  result_of_dice_roll;
    3.  
    at the very end of the method.

    So, forget about the "passing in a parameter" part if that's not what you want. That was just to explain why Unity was confused about the "takes 1 argument" part. More important is the
    return  
    part, so that when this function finishes, it spits out whatever the result is with
    return
    .
     
    Last edited: Feb 7, 2020
  19. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Ok but the program isn't going to know what result_of_dice_roll; means. And I don't know what you mean by = whatever.

    Also you can't return a value from an iterator. So return doesn't work inside it That's the whole problem with the dice script I'm using. The random.range doesn't need to be in an iterator but the "animation" to make the sides change does. But by sticking the random.range inside the iterator it's preventing the results from being seen outside of the method.
     
    Last edited: Feb 9, 2020
  20. rallyall

    rallyall

    Joined:
    Dec 20, 2019
    Posts:
    36
    Here's the solution. In my particular I replace line 72:
    Code (CSharp):
    1. finalZoneSide = randomDiceSide + 1;
    with
    Code (CSharp):
    1. GameController.finalZoneSide = randomDiceSide + 1;
    GameController is the name of the script that will be using it. finalZoneSide could have been anything, blahblah, would work as long as you stay consistent.

    Then in the GameController script I add outside of any method
    Code (CSharp):
    1. public static int finalZoneSide = 0;