Boolean won't set false?
Hi all,
I've spent the past few days trying to figure this out and have gotten no where. Here's what's going on:
I'm making a peg and hole game. I have three scripts, Hole (attached to hole gameobjects), LevelManager (attached to an empty gameobject that's always there), and GameBoard (attached to the gameboard object). The LevelManager instantiates a game board and right now the game board is hard coded to set up as triangle with sides of length 5. The gameboard then instantiates the correct number of holes and places them in the correct place. All of this works great.
The problem I'm having is this: When a hole is Instantiated it calls the method 'activatePeg' and the bool 'hasPeg' is set true. This works fine for all 15 holes.
What doesn't work is this: once everything is set up, 'deactivatePeg' is called on one particular hole. I'm getting a null exception on the levelManager decrement line in the method (even though the levelManager is present in the inspector and it was previously used in the 'activatePeg' method), and even if I comment that out the bool is not setting false. The hole I'm calling the method on does exist, the method does run (I've had print statements in it), it just doesn't do anything. I'm thinking it's a reference error somewhere, but I can't figure it out. Any ideas?
Here is part of the Hole script:
public class Hole : MonoBehaviour {
public bool isPegActive;
public float xPos;
public float yPos;
public int xCoord;
public int yCoord;
public LevelManager levelManager;
public GameObject peg;
// Use this for initialization
void Start () {
levelManager = FindObjectOfType<LevelManager>();
xPos = transform.position.x;
yPos = transform.position.y;
activatePeg();
}
// Update is called once per frame
void Update () {
}
public void activatePeg()
{
isPegActive = true;
levelManager.numberOfPegs += 1;
peg.gameObject.SetActive(true);
}
public void deactivatePeg()
{
isPegActive = false;
levelManager.numberOfPegs -= 1;
peg.gameObject.SetActive(false);
}
}
Here is part of the gameboard script. The Vector3 array is just positions for every hole. All of that works perfectly.
public void setupBoard(string boardType)
{
switch (boardType)
{
case "Base5Triangle":
// Sets the sprite of this particlar game board
gameObject.GetComponent<SpriteRenderer>().sprite = spriteArray[0];
currentBoard = boardType;
transform.localScale = new Vector3(0.7f, 0.7f, 1f);
width = 5;
height = 5;
//Hard coded positions of all of the holes for this particular game board
Vector3[,] base5PositionsArray = new Vector3[,] {{new Vector3(.0f, 1.63f, 0f), Vector3.zero, Vector3.zero, Vector3.zero, Vector3.zero},
{new Vector3(-0.47f, 0.88f, 0f), new Vector3(0.47f, 0.88f, 0f), Vector3.zero, Vector3.zero, Vector3.zero},
{new Vector3(-0.939f, 0.009f, 0f), new Vector3(0f, 0.009f, 0f), new Vector3(0.939f, 0.009f, 0f), Vector3.zero, Vector3.zero},
{new Vector3(-1.41f, -0.81f, 0f), new Vector3(-0.47f, -0.81f, 0f), new Vector3(0.47f, -0.81f, 0f), new Vector3(1.41f, -0.81f, 0f), Vector3.zero},
{new Vector3(-1.81f, -1.63f, 0f), new Vector3(-0.939f, -1.63f, 0f), new Vector3(0f, -1.63f, 0f), new Vector3(0.939f, -1.63f, 0f), new Vector3(1.81f, -1.63f, 0f)}};
// Instantiates holes and addes them into the appropriate positions of the 2D hole array
// Position in array is null if there is no hole there.
holeArray = new GameObject[height, width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j <= i)
{
holeArray[i,j] = Instantiate(holeAndPeg, base5PositionsArray[i, j], Quaternion.identity, gameObject.transform);
}
else
{
holeArray[i, j] = null;
}
}
}
// Deactivates the top most peg to start the game
holeArray[0, 0].GetComponent<Hole>().deactivatePeg();
break;
default:
break;
}
}
Your answer
Follow this Question
Related Questions
Static function not working in between screen 2 Answers
getting a NullReferenceException error and can't see why. 1 Answer
How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers
NullReferenceException in FiniteStateMachine with ThirdPersonCharacter 0 Answers
Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer