How to get scripts to detect foreign variable change?
Here's a quick summary beforehand to make it easier:
I have a GameManager script and several other scripts attatched to different prefabs. There is one integer in GameManager that is called "level" and it increases by 1 once certain win conditions have been met. In each prefab's script, in their OnTriggerEnter methods, there are "if" statements that say if GameManager.level == 1 then A happens. Else, if GameManager.level == 2 then B happens. For some reason, after GameManager.level increases to 2, these prefab scripts don't recognize the change and "level" 2 cannot be beat.
Here is the problem in greater detail:
I am making a burger minigame where the player is required to place the correct ingredients on the table in order to get points. I have a GameManager with a public integer named "level" that keeps track of which "level" the player is on (starting with 1). In the script for each individual food item prefab (buns, meat, cheese, etc.) the GameManager script is referenced using the following code:
// bottom bun script public GameObject GameManager; private GameManager script; private void Start() { script = GameManager.GetComponent(); }
There are buttons on the UI that spawn new food items when clicked, each item being an instance of the prefab with a script attached (all bottom bun instances share the bottomBun script, and so on). In the OnTriggerEnter method in each food script, I am using "if" statements to determine which level it is and which ingredients are required. For example, if (script.level == 1) then the ketchup must be touching the cheese in order to get a point for having that ingredient. else, if (script.level == 2), the onions must be touching the pickle in order to get a point. It's kind of hard to explain, but just know it works perfectly for the first level. Whenever a food item is placed on the table (or another food item), it's supposed to give the player a point (assuming that ingredient is required during that level) and once all the required ingredients have been placed on the burger and the player has earned enough points (1 per correct item, a total of 4 for the first "level"), the GameManager will erase all food items and increase the "level" integer by 1. I know that "level" is changing because when I Debug.Log(level) it changes to "2" after the first burger is complete.
However, during level 2 (when the GameManager says level = 2), once the new food items are spawned in they don't recognize that script.level = 2. In the OnTriggerEnter methods for these scripts, I put Debug.Log(script.level), but it comes out as "1" even if the GameManager script says that level = 2. I don't know if I linked the GameManager to these scripts incorrectly, or if it has something to do with the food items being destroyed and respawned after level 1 is completed. I apologize for my horrible job explaining the issue, but does anyone have any idea what might be going on here?
Here are the relevant scripts:
GameManager
Bottom Bun
Your answer
Follow this Question
Related Questions
Game Objects not using same script independently. 1 Answer
[Solved]Weird Time.timeScale behavior 3 Answers
Having trouble moving a GameObject to another scene and Transform it to a different position 0 Answers
Problem while using several tags in a OnTriggerEnter (Collider other) 0 Answers
Unity 5 Accessing other scripts problem 2 Answers