Hello,
I’m currently making my first game which consists of a simple roll the dice board game like monopoly and when I tryedr to change it from 2 players to a dynamic number of players (i haven’t made the system to add players yet so I’ve been manually adding to an array just to test this first).
The issue i am facing is that when i roll the dice for the first time the player1 doesn’t move, but after that the game works fine has when it comes back to the turn of the 1st player it moves and so on until the game is over.
I’ve been trying to identify this issue with debug.log and trying various tweaks to the code, but i still can’t solve this.
I think the issue is in the coroutine on Dice.cs but has i am not sure i will provide the main two scripts:
Dice.cs:
using System.Collections;
using UnityEngine;
public class Dice : MonoBehaviour
{
private Sprite[] diceSides;
private SpriteRenderer rend;
private int whosTurn = 0;
private bool coroutineAllowed = true;
private void Start()
{
rend = GetComponent<SpriteRenderer>();
diceSides = Resources.LoadAll<Sprite>("DiceSides/");
rend.sprite = diceSides[5];
}
private void OnMouseDown()
{
if (!GameControl.gameOver && coroutineAllowed)
{
GameControl.CheckTurn(whosTurn);
StartCoroutine(RollTheDice());
}
}
private IEnumerator RollTheDice()
{
coroutineAllowed = false;
int randomDiceSide = 0;
for (int i = 0; i <= Random.Range(10, 25); i++)
{
randomDiceSide = Random.Range(0, 5);
rend.sprite = diceSides[randomDiceSide];
yield return new WaitForSeconds(0.05f);
}
GameControl.diceSideThrown = randomDiceSide + 1;
coroutineAllowed = true;
whosTurn += 1;
if (whosTurn == GameControl.players.Length)
{
whosTurn = 0;
}
GameControl.StopMovement(GameControl.players[whosTurn]);
GameControl.CheckGameOver(GameControl.players[whosTurn]);
}
}
And GameControl.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Analytics;
public class GameControl : MonoBehaviour
{
public GameObject[] gameObjects;
public static Player[] players;
public static int diceSideThrown = 0;
public static bool gameOver = false;
void Start()
{
InitializePlayers();
gameOver = false;
}
//Initializes players that ahve with unique id, gameobject and it's first waypointindex
private void InitializePlayers()
{
players = new Player[gameObjects.Length];
int i = 0;
foreach (GameObject gameObject in gameObjects)
{
players[i] = new Player(i, gameObject, 0);
players[i].gameObject.GetComponent<FollowThePath>().moveAllowed = false;
i++;
}
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < players.Length; i++)
{
if (players[i].gameObject.GetComponent<FollowThePath>().moveAllowed)
{
StopMovement(players[i]);
CheckGameOver(players[i]);
}
}
}
public static void CheckGameOver(Player player)
{
if (player.gameObject.GetComponent<FollowThePath>().waypointIndex == player.gameObject.GetComponent<FollowThePath>().waypoints.Length)
{
gameOver = true;
}
}
public static void StopMovement(Player player)
{
Debug.Log("move " + (player.startWaypoint + diceSideThrown) + " tiles");// dá 0 no primeiro clique
Debug.Log("Index" + player.gameObject.GetComponent<FollowThePath>().waypointIndex);
if (player.gameObject.GetComponent<FollowThePath>().waypointIndex > player.startWaypoint + diceSideThrown)
{
player.startWaypoint = player.gameObject.GetComponent<FollowThePath>().waypointIndex - 1;
player.gameObject.GetComponent<FollowThePath>().moveAllowed = false;
}
}
public static void CheckTurn(int playerToMove)
{
Debug.Log("Check turn" + players[playerToMove].id);
players[playerToMove].gameObject.GetComponent<FollowThePath>().moveAllowed = true;
}
}
I hope you guys can help and be patient with me has i’m new to Unity and CSharp ![]()