I have a game that creates and places a random number of "pickups" on a plane at random places.
I then have a script on the "Player" game object that uses OnTriggerEnter to tell when an item has been "picked up" and compares a count (the score) to the number of objects that are created:
void OnTriggerEnter(Collider other)
{
// Get all game objects with the Tag "Pickup"
if (other.gameObject.CompareTag ("Pickup")) {
// Deactivate objects that are touched/collected
other.gameObject.SetActive (false);
// add to count
count++;
scoreText.text = "Score: " + count;
//TODO: fix this statement; pickupCount is always 0 here, why?
if(count >= pickupCount){
winText.text = "You Win!";
}
}
}
The problem I am facing is that during building this worked fine, I built the project and it outputted "You Win!" after picking up 1 "pickup", I found out this is because for some reason the value of pickupCount is now always 0.
pickupCount = GameObject.FindGameObjectsWithTag("Pickup").Length;
My guess is that because the Playerscript checks at Start() and the SpawnScript creates at Start() that the reason the number is always 0 is because at the time of checking there are in fact no "pickups" created. My question is then, how do I check for the total number of these pickups without doing it in an update function?
SIDE NOTE
I know I could just change the OnTriggerEnter() function to be something like this:
void OnTriggerEnter(Collider other)
{
// Get all game objects with the Tag "Pickup"
if (other.gameObject.CompareTag ("Pickup")) {
// Deactivate objects that are touched/collected
other.gameObject.SetActive (false);
// check number of pickups left
pickupCount = GameObject.FindGameObjectsWithTag("Pickup").Length;
// add to count to show the score
count++;
scoreText.text = "Score: " + count;
if(pickupCount <= 0){
winText.text = "You Win!";
}
}
}
But it is more a case of curiosity now more than anything. Is what I am asking even possible?