Hi,
I originally posted something similar to this in a different thread, but I think my original question of how to end the coroutine was off base. My main problem is, in a Yahtzee-type game, I’m not quite sure how to end this one player’s turn and reset the dieButtonLocked.isOn to false again. I made a method (NextMove) but no matter where I try it, I either get the dice rolling automatically when the game starts, or they stay locked and it only performs one turn. I’m also getting a side problem of the player being allowed to move multiple times ( the same totalRolledAmount just by clicking the RollButton again after the player stops moving). My main focus right now though is ending the one turn and getting the state of the dice to be unlocked again. Thank you in advance for your time looking. I posted 3 scripts below:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class GameControl : MonoBehaviour
{
private static GameObject player1;
public static int totalRolledAmount = 0; //current move spaces allowed
public static int player1StartWaypoint = 0;//starting position
public static bool gameOver = false; // if True, game is over
public static bool playerMoved = false;
//3-14---------------------------------
public RandomDice randomDice;
//--------------------------------------
void Start()
{
player1 = GameObject.Find("Player1");
player1.GetComponent<FollowThePath>().moveAllowed = false;
//3-14--------------------------------
//randomDice.RandomDiceRoll(); // starts the dice rolling before thr Roll Button is clicked
//-----------------------------------
}
void Update()
{
if (player1.GetComponent<FollowThePath>().waypointIndex >
player1StartWaypoint + totalRolledAmount) // begins at the starting waypoint and moves the number rolled on dice
{
player1.GetComponent<FollowThePath>().moveAllowed = false; //Inhibits player from moving from starting Waypoint to the last waypoint on single move
player1StartWaypoint = player1.GetComponent<FollowThePath>().waypointIndex - 1;//this now becomes Starting Waypoint for next turn
}
if (player1.GetComponent<FollowThePath>().waypointIndex ==
player1.GetComponent<FollowThePath>().waypoints.Length) //If player reaches the last waypopint, whoWinsText will appear
{
Debug.Log("Out Of Bounds");
}
}
public static void MovePlayer(int totalRolledAmount)
{
player1.GetComponent<FollowThePath>().moveAllowed = true;
playerMoved = true;
}
public void NextMove()
{
if (playerMoved == true)
{
player1.GetComponent<FollowThePath>().moveAllowed = false;
randomDice.die1ButtonLocked.isOn = false;
randomDice.die2ButtonLocked.isOn = false;
randomDice.die3ButtonLocked.isOn = false;
randomDice.StartCoroutine("PickRandomDice");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class RandomDice : MonoBehaviour
{
public Sprite[] DiceHolder;
public Sprite[] DiceDirectionHolder;
public Image Die1;
public Image Die2;
public Image Die3;
public int redDice1ChosenFromTheArray, redDice2ChosenFromTheArray, redDice3ChosenFromTheArray;
public int totalRolledAmount;
public Toggle die1ButtonLocked;
public Toggle die2ButtonLocked;
public Toggle die3ButtonLocked;
int numberOfRolls = 0;
public bool IsDoneRolling = false;
[SerializeField] TextMeshProUGUI rollText;
private bool coroutineAllowed = true;
/// <summary>
/// Sets the Toggle to Off so it doesn't stay locked from previous roll
/// <summary>
public void Start()
{
die1ButtonLocked.isOn = false;
die2ButtonLocked.isOn = false;
die3ButtonLocked.isOn = false;
rollText.text = totalRolledAmount.ToString();
}
public void RandomDiceRoll()
{
if (die1ButtonLocked.isOn && die2ButtonLocked.isOn && die3ButtonLocked.isOn || numberOfRolls == 3)
{
IsDoneRolling = true;
GameControl.MovePlayer(totalRolledAmount);
}
else
{
GetComponent<AudioSource>().Play();
StartCoroutine("PickRandomDice");
}
}
IEnumerator PickRandomDice()
{
if (!die1ButtonLocked.isOn)
{
int die1Repeater = (Random.Range(3, 4));
float waitTime1 = (Random.Range(0.05f, 0.1f));
for (int i = 0; i < die1Repeater; i++)
{
redDice1ChosenFromTheArray = Random.Range(0, DiceHolder.Length);
Die1.sprite = DiceHolder[redDice1ChosenFromTheArray];
yield return new WaitForSeconds(waitTime1);
Debug.Log(redDice1ChosenFromTheArray + 1);
}
}
if (!die2ButtonLocked.isOn)
{
int die2Repeater = (Random.Range(3, 5));
float waitTime2 = (Random.Range(0.05f, 0.1f));
for (int i = 0; i < die2Repeater; i++)
{
redDice2ChosenFromTheArray = Random.Range(0, DiceHolder.Length);
Die2.sprite = DiceHolder[redDice2ChosenFromTheArray];
yield return new WaitForSeconds(waitTime2);
Debug.Log(redDice2ChosenFromTheArray + 1);
}
}
if (!die3ButtonLocked.isOn)
{
int die3Repeater = (Random.Range(3, 6));
float waitTime3 = (Random.Range(0.05f, 0.1f));
for (int i = 0; i < die3Repeater; i++)
{
redDice3ChosenFromTheArray = Random.Range(0, DiceHolder.Length);
Die3.sprite = DiceDirectionHolder[redDice3ChosenFromTheArray]; // 2_26 changed from DiceHolder to DiceDirectionHolder
yield return new WaitForSeconds(waitTime3);
Debug.Log(redDice3ChosenFromTheArray);
}
}
totalRolledAmount = ((redDice1ChosenFromTheArray + 1) + (redDice2ChosenFromTheArray + 1));
rollText.text = totalRolledAmount.ToString();
Debug.Log("The total of the 2 dice = " + totalRolledAmount); // Displays the Total of the Dice in Console
numberOfRolls++;
Debug.Log(" The number of Rolls = " + numberOfRolls);
GameControl.totalRolledAmount = ((redDice1ChosenFromTheArray + 1) + (redDice2ChosenFromTheArray + 1));
if (die1ButtonLocked.isOn && die2ButtonLocked.isOn && die3ButtonLocked.isOn || numberOfRolls == 3)
{
IsDoneRolling = true;// 3_13: If if deactivate this line player can roll more than 3x
}
// 3-14 coroutineAllowed = true; Deactivating this does not seem to have any effect
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowThePath: MonoBehaviour {
public Transform[] waypoints;
[SerializeField]
private float moveSpeed = 1f;
[HideInInspector]
public int waypointIndex = 0;
public bool moveAllowed = false;
// Use this for initialization
void Start ()
{
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
void Update ()
{
if (moveAllowed)
Move();
}
private void Move()
{
if (waypointIndex <= waypoints.Length - 1)
{
transform.position = Vector2.MoveTowards(transform.position, waypoints [waypointIndex].transform.position, moveSpeed * Time.deltaTime);
if (transform.position == waypoints [waypointIndex].transform.position)
{
waypointIndex += 1;
}
}
}
}