My gameplay is such that I want my scenes to perform this flow:
I have 4 different triggers leading to 4 diffferent scenes. But every time I start Scene1, the scene which gets instantiated on coming in contact with any of the triggers for the first time, just gets instantiated again no matter whichever scene the next trigger leads to.
What am I doing wrong here?
How to solve it?
The colliders that you see in the picture above are the 4 triggers. And the code on each of these trigger is below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class enter4kWorld : MonoBehaviour {
//Orb Transform
public GameObject targetGO;
public GameObject otherTargetGO1;
public GameObject otherTargetGO2;
public GameObject otherTargetGO3;
public Animator LightBurst;
public GameObject Processor;
public GameObject SceneExitGO;
public float CountDownTimer;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "GameController")
{
Debug.Log("Teleporter works");
SceneExitGO.SetActive(true);
targetGO.SetActive(true);
otherTargetGO1.SetActive(false);
otherTargetGO2.SetActive(false);
otherTargetGO3.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if (SceneExitGO.activeSelf)
{
//float step = speed * Time.deltaTime;
//transform.position = Vector3.MoveTowards(transform.position, target.position, step);
CountDownTimer -= Time.deltaTime;
if (Processor.transform.position.y <= 9f)
{
Processor.transform.Translate(Vector3.up * Time.deltaTime * 1.5f, Space.World);
}
if (CountDownTimer <= 4.5)
{
LightBurst.SetTrigger("TriggerLightBurst");
}
if (CountDownTimer <= 0)
{
ChangeScene();
CountDownTimer = 0;
}
}
}
public void ChangeScene()
{
SceneManager.LoadScene("Scene2");
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Thanks to a friend I came to solve this problem. The problem here is that all the scripts attached to all the triggers are similar and if you look closely, all of them contain a same public GameObject that is SceneExitGO. As soon as the SceneExitGO gets active, all the scripts attached to each trigger gets activated to perform their respective actions in Update function. This condition is called "race condition", when multiple components or scripts are running simultaneously to achieve an outcome there occurs a conflict. I solved this just by changing the public GameObject to a private Bool.