- Home /
,Script only working for one objects and not the others
I'm trying to make a board game for my game prod class. We only have 2 weeks to put a prototype together but I'm having an issue with my NPCs and getting them to all move randomly at the end of each turn. Currently there are 11 NPCs and they all have the same script attached. They all move to their start positions but then only one of them moves each time. Considering I don't have much time I might just make 11 versions of the script but I also know there's a fix to the issue. (Also the boundaries system is a bit iffy but that's not a main concern rn I can fix it later)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NPCManager : MonoBehaviour
{
//Variables
//Movement
public float startX;
public float startY;
private int dice; //Random Generated Number 1-4 to decide what direction they'll move in
private float move = 1.37f;
private float minY = -6.79f; //Down
private float maxY = 6.91f; //Up
private float minX = -9.63f; //Left
private float maxX = 9.54f; //Right
//Turn System
public static bool npcTurn = false;
private int turnChoice; //Random Generated Number 1-10 to decide who goes next
// Start is called before the first frame update
void Start()
{
//Place NPC
transform.position = new Vector2(startX, startY);
Debug.Log("NPC Start: " + gameObject.name);
}
//Randomly Move NPCs
private void MoveNPC()
{
if (npcTurn == true)
{
dice = Random.Range(1, 4);
switch (dice)
{
//Left
case 1:
if (startY > minY || startY == minY)
{
startY += move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
else
{
startY -= move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
break;
//Up
case 2:
if (startX < maxX || startX == maxX)
{
startX -= move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
else
{
startX += move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
break;
//Right
case 3:
if (startY < maxY || startY == maxY)
{
startY -= move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
else
{
startY += move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
break;
//Down
case 4:
if (startX > minX || startX == minX)
{
startX += move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
else
{
startX -= move;
transform.position = new Vector2(startX, startY);
npcTurn = false;
turnChoice = Random.Range(1, 10);
Debug.Log("Turn Choice #" + turnChoice);
if (turnChoice < 5)
{
BlueCultist.blueTurn = true;
Debug.Log("Blues Turn After NPC");
}
else
{
OrangeCultist.orangeTurn = true;
Debug.Log("Oranges Turn After NPC");
}
}
break;
}
}
}
// Update is called once per frame
void Update()
{
MoveNPC();
}
}
Answer by Krixion · Jan 26, 2020 at 11:08 PM
It's because you are using a static bool for the NPC turn. Static variables are a global definition so changing it to true on one NPC will change it to true on all of your NPC objects.