Boolean variable won't trigger in other script
I have a variable hitTrigger that is set to false by default, and turns true once my player collides with an object. Here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxCollide : MonoBehaviour {
public GameObject CubeObj; // cube object
public Camera Stall; // stall camera
public GameObject Player; // our fps controller
public GameObject Ball;
public AudioListener listener; // this listener needs to me ON when we deactivate
// the FPS controller
public bool hitTrigger = false;
void Start() {
// referenced variables
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
CubeObj = GameObject.Find("Stall Collider");
Player = GameObject.Find("FPSController");
Ball = GameObject.Find("Sphere");
// now these objects are inside the variables
if (hit.gameObject.name == "Stall Collider")
{
Debug.Log("Walked on!");
Destroy(CubeObj); // we destory the box collision cube
Debug.Log("Removed!");
Stall.enabled = true; // enables the stall camera
if (Stall.enabled == true) {
Player.SetActive(false); // when we enable stall, we disable FPS
}
Debug.Log("Camera Active!");
listener.enabled = true; // we switch on the new listener
playMainMusic.Stop(); // and stop the main music
Debug.Log("Stopped Main Music");
playStallMusic.Play(); // then play the new music
Debug.Log("Playing Stall Music");
hitTrigger = true; // hitTrigger TURNS TRUE HERE
}
if (hitTrigger == true) {
Debug.Log("hitTrigger: true");
// just to be sure the boolean turns true
}
}
}
I then have another script called fisherTimerv2.cs that I need to do something once hitTrigger has turned true. Here's the fisherTimerv2.cs Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fisherTimerv2 : MonoBehaviour {
bool countStop = false;
int counter = 0;
public Spawn spawnNewCharacter;
void Start()
{
StartCoroutine(Example());
}
IEnumerator Spawning()
{
if (GameObject.Find("FPSController").GetComponent<BoxCollide>().hitTrigger == true)
{
Debug.Log("CUSTOMER SHOULD SPAWN!");
while (countStop == false) {
yield return new WaitForSeconds(2);
Debug.Log("Fisher Spawned!");
counter++;
spawnNewCharacter.SpawnCharacter();
if (counter >= 3){
countStop = true;
}
}
}
}
}
As you can see in the IEnumerator Spawning() function, I have an if statement that checks if hitTrigger turns true, then does whatever afterwards, but for some reason I can't get that check in the Spawning() function to work at all, it just completely ignores it.
Any reason for this?
Answer by Backside2357 · Jan 04, 2018 at 06:14 PM
You should do more debugging first. To do this look at this line
if (GameObject.Find("FPSController").GetComponent<BoxCollide>().hitTrigger == true)
and split it up so you can debug every single part of it:
FPSController FPSC = GameObject.Find("FPSController");
BoxCollide BOXC = FPSC.GetComponent<BoxCollide>();
bool hitTrigger = BOXC.hitTrigger;
if (hitTrigger == true)
You can now use Debug.Log
to look at individual object properties and check if they are what you expect (is FPSC the right FPSController? Is BOXC the right BoxCollide script?). You should also use Debug.Log
calls to check if the Spawning()
function is even called and if so, if it is called in the right order. Do also a log output in the BoxCollide
function to log to the console what the actual value of hitTrigger
is.
I think I've found the issue, as soon as I run my game, the function gets called straight away, which means it tries to access the if statement but cannot since hitTrigger
is false. Is there anyway I can change the code just so the Spawning()
function itself gets called when hitTrigger
is true ins$$anonymous$$d?