Detect collision on an object and send the data to a game manager to display text and enable/disable objects on that level
Hello, In my game, I have my player kill all enemies present in a level and then have a passage open up and a text pop up. Within the passage there is a chest that is there that can be opened and presents text when close enough. When the chest is opened there is a key that falls to the floor and has another text appear. Finally when the key is picked up it shows up on the side of the players screen so he can walk up to the door and open it. This worked completely fine with a player that was used on the level. The script that is used has tons of public variables that I had dropped on the inspector to make it work. But when I ran my game from the start, I used DontDestroyOnLoad to bring over the same health and ammo count from the player from the last level. Causing the script that was attached to the player to no longer have all of the game objects attached to the script. So I tried moving the script over to an empty object called game manager. That didn't work though since there was no collision detection between my player and the blocks. So I wrote a code on my player that detects collision with the blocks and calls a function in the game manager script. Sadly nothing has been working and I am stuck.
Game Manager Script:
public void TouchingChest(bool check)
{
Debug.Log(check);
if (check == true)
{
chestImage.gameObject.SetActive(true);
chestText.gameObject.SetActive(true);
openChest = true;
}
else
{
chestImage.gameObject.SetActive(false);
chestText.gameObject.SetActive(false);
openChest = false;
}
}
public void TouchingKey(bool check)
{
Debug.Log(check);
if (check == true)
{
keyIamge.gameObject.SetActive(true);
keyText.gameObject.SetActive(true);
pickupKey = true;
}
else
{
keyIamge.gameObject.SetActive(false);
keyText.gameObject.SetActive(false);
openChest = false;
}
}
public void TouchingDoor(bool check)
{
Debug.Log(check);
if (check == true)
{
openDoor.gameObject.SetActive(true);
openDoors.gameObject.SetActive(true);
useKey = true;
}
else
{
openDoor.gameObject.SetActive(false);
openDoors.gameObject.SetActive(false);
useKey = false;
}
And here is the player script for the collision detection:
public void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.name == "NextSceneTrigger")
{
level.NextLevel();
}
if (collider.gameObject.name == "Chest")
{
Debug.Log("Chest");
other.TouchingChest(true);
}
if (collider.gameObject.name == "Key")
{
Debug.Log("Key");
other.TouchingKey(true);
}
if (collider.gameObject.name == "TheDoor" || collider.gameObject.name == "TheDoor2" || collider.gameObject.name == "TheDoor3")
{
Debug.Log("Door");
other.TouchingDoor(true);
}
}
private void OnTriggerExit2D(Collider2D collider)
{
if (collider.gameObject.name == "Chest")
{
Debug.Log("Chest");
other.TouchingChest(false);
}
if (collider.gameObject.name == "Key")
{
Debug.Log("Key");
other.TouchingKey(false);
}
if (collider.gameObject.name == "TheDoor" || collider.gameObject.name == "TheDoor2" || collider.gameObject.name == "TheDoor3")
{
Debug.Log("Door");
other.TouchingDoor(false);
}
}
Ignore the NextSceneTrigger. If anyone can figure out whats wrong with this code or if there are any better alternatives that would be much appreciated. Thank you in advanced.
Your answer
Follow this Question
Related Questions
How to snap a placeable object on wall & move on to other walls of a room? 0 Answers
How to detect collision of two moving characters? 1 Answer
Changing light color between players when they collide (BOLT) 0 Answers
Problem replaying exactly same movement 1 Answer
How do I get nav agents to not collide with each other? 0 Answers