- Home /
How to make a script work in scene1 only if a collider in scene2 is triggered
So, I'm making this game that has a Level Select screen with few levels and then each of those levels has a scene. This is the LevelSelect scene: And this is the script attached to the LevelManager in this scene:
private void Start()
{
PlayerPrefs.SetInt("Scene1stars", 3);
for (int i = 0; i < levelLoaders.Length; i++)
{
if(i == 0)
{
if(PlayerPrefs.GetInt("Scene1stars") == 0)
{
levelLoaders[i].interactable = false;
}
for (int j = 0; j < starsOne.Length; j++)
{
if (PlayerPrefs.GetInt("Scene2stars") > j)
{
starsOne[j].gameObject.SetActive(true);
}
}
}
if (i == 1)
{
if (PlayerPrefs.GetInt("Scene2stars") == 0)
{
levelLoaders[i].interactable = false;
}
for (int j = 0; j < starsTwo.Length; j++)
{
if (PlayerPrefs.GetInt("Scene3stars") > j)
{
starsTwo[j].gameObject.SetActive(true);
}
}
}
if (i == 2)
{
if (PlayerPrefs.GetInt("Scene3stars") == 0)
{
levelLoaders[i].interactable = false;
for (int j = 0; j < starsThree.Length; j++)
{
if (PlayerPrefs.GetInt("Scene4stars") > j)
{
starsThree[j].gameObject.SetActive(true);
}
}
}
}
if (i == 3)
{
if (PlayerPrefs.GetInt("Scene4stars") == 0)
{
levelLoaders[i].interactable = false;
}
}
}
}
This is a scene of the level1 for example (three stars on level 1 are just example of what happens if all three points in level 1 are collected): It has this script attached to the "points":
public class PickUpPoints : MonoBehaviour
{
public string levelName;
private void Start()
{
levelName = SceneManager.GetActiveScene().name;
}
private void OnTriggerEnter(Collider other)
{
PlayerPrefs.SetInt("Scene" + (SceneManager.GetActiveScene().buildIndex + 1 )+ "stars", PlayerPrefs.GetInt("Scene" + (SceneManager.GetActiveScene().buildIndex + 1) + "stars") + 1);
}
}
Now, the problem is, if I for example click on level1 in the Level Selection screen to start the level1 scene and then while playing collect two points I will get two stars in the Level Selection scene even if I quit the "play" in the editor or touch a game over colliders (up or bottom black lines). I would like to make those stars appear in the Level Selection scene only if the player touches the Level finish collider, otherwise, I would like those points and stars not to be saved in the playerprefs. How can I do that?
Answer by Llama_w_2Ls · Jan 30, 2021 at 12:26 PM
You could use a static script and a start script to keep track of bools in different scenes. For example:
public static class ControlBools
{
public bool ColliderWasTriggered;
}
Then, when you enter the collider in Scene2, you can set the bool to true. For example:
void OnCollisionEnter(Collider collision)
{
// do collision stuff
ControlBools.ColliderWasTriggered = true;
}
Finally, whenever you load Scene1, there needs to be a script that has a reference to the script you want to enable/disable, depending on if the collider was entered in scene2. For example:
public class StartScript : MonoBehaviour
{
public Scene1Script script;
void Start()
{
if (ControlBools.ColliderWasTriggered)
script.enabled = false;
}
}