- Home /
How do I get an object to send a value to a manager script using an ontriggerenter?
I have an object with a script on it called SafeZone. I want this script to return true when entered by the player tagged game object. so I did:
Then on my ManagerScript I want to check the Boolean from SafeZone and load a new scene when its true.
I have a reference to the SafeZone script on my ManagerScript by making the gameobject a prefab and setting it in the editor via the inspector.
I set up my levels by giving them a curLevel and nextLevel name and set them to the name needed to access the next level in the build. It works when on the zone itself. SceneManager.Loadscene(nextScene.name);
nothing works. I made sure CheckSafe(); is called from update method I also added a debug line to the function and it doesn't get output to the console window either. However, the boolean on the game object will still toggle when the player object enters it. It just isn't sending that value to the GameManager script. At the moment I kept it on the SafeZone gameObject and it loads the next scene just fine, but I don't know if this is the best way to do this. Can anyone tell me what i'm doing wrong?
The reason I want to keep it all on the GameManager is I also have a public int for lives on the script as well, and a deathzone. I want to reset the level, not reload which is another beast I'm working on implementing, and decrease the lives value by 1. Same deal with the script an OTE that checks for the player on a separate game object. That when entered should return the boolean entered as true, and decrease the lives value by one or lives--; and reset the level function Reset(); so something like CheckDeath(){ if (DeathZone.entered == true) { lives--; Reset();}}
Sorry about the mess I gave this spaces and everything but It got bunched together when submitted. I don't have the screen grabs at the moment or I would add them. This is all just from memory of the issue. I've been doing a lot of research and dabbling in this area to make this function work, but I can't seem to get the scripts to send information updates to each other when two separate game object's collision happens.
Answer by Zentiu · Mar 02, 2020 at 01:16 PM
If you gamemanager handles the loading then this is what i usually do:
I assume there should always be 1 game manager.
public class GameManager : Monobehaviour
{
public static GameManager manager;
public bool isInSafeZone = false;
public Start ()
{
manager = this;
}
public void LoadScene (int nextScene)
{
SceneManager.LoadScene (nextScene);
}
}
And then in your script that checks if the player enters the safeZone:
Public void OnTriggerEnter (collision other)
{
if(other.gameObject.compareTag ("Player") == true)
{
GameManager.manager.isInSafeZone = true;
GameManager.manager.LoadScene(1);
}
}
Ofcourse you can let the method LoadScene activate somewhere else instead of when the player enters te trigger. But this should work.