- Home /
Door error
Sooooo, I have a script on one of my doors in my game that allows the key im holding to open the door and allow me to move onto the next scene by going through the door. It was working previously after I saved it last week. I made a few more changes to my game for an assignment for school that I did not need in my game, so after I submitted that assignment I clicked "do not save changes." If anyone can help me it would be much appreciated. Also I am very new to scripting :)
Here is the script for the door:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Door : MonoBehaviour {
private PlayerAnimController thePlayer;
public SpriteRenderer theSR;
public Sprite doorOpenSprite;
public bool doorOpen, waitingToOpen;
// Start is called before the first frame update
void Start()
{
thePlayer = FindObjectOfType<PlayerAnimController>();
}
// Update is called once per frame
void Update()
{
if (waitingToOpen)
{
if (Vector3.Distance(thePlayer.followingKey.transform.position, transform.position) < 0.1f)
{
waitingToOpen = false;
doorOpen = true;
theSR.sprite = doorOpenSprite;
thePlayer.followingKey.gameObject.SetActive(false);
thePlayer.followingKey = null;
}
}
if (doorOpen && Vector3.Distance(thePlayer.transform.position, transform.position) < 1f && Input.GetAxis("Vertical") > 0.5f)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
if (thePlayer.followingKey != null)
{
thePlayer.followingKey.followTarget = transform;
waitingToOpen = true;
}
}
}
}
Answer by Xquality · Oct 27, 2021 at 08:19 AM
Could it be that you're not pressing the "w" or "up key" when you're within 1 unit of the open door? Or does the door not open at all?
The door opens and I am pressing the up key or "w" when im within 1 unit of the door. So it is really odd to me why it is not working.
I also have the scenes in proper order In the build settings.
Hm perhaps you could send me the project if it's not too big that is. I'm not sure what's going wrong xd
Could there possibly be something wrong with my Game Scene Manager. I added a script that my professor told me to add and that works in loading the next screen, however, he said that the "use game scene manager" box should be checked but it only works when it's not checked. While it works in loading the next scene, it does not use the key and open door functions I want it to use. Here is that script for the manager:
public class GameSceneManager : MonoBehaviour { //Like the GameManager, this should be it's own gameobject
[Tooltip("The black screen transition that will be used")]
public GameObject Transition;
[Tooltip("If you want to open this scene with a fade in")]
public bool startWithFadeIn = true;
// Start is called before the first frame update
void Start()
{
if (startWithFadeIn)
{
StartCoroutine(FadeIn());
}
}
//This function should be called to other scripts so that way you have the transition working
public void LoadScene(int SceneIndex)
{
StartCoroutine(FadeOut());
StartCoroutine(LoadAsyncScene(SceneIndex));
}
IEnumerator LoadAsyncScene(int SceneIndex)
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(SceneIndex);
// Wait until the asynchronous scene fully loads
while (!asyncLoad.isDone)
{
yield return null;
}
}
public IEnumerator FadeIn()
{
Transition.SetActive(true);
Transition.GetComponent<Animator>().SetBool("FadeIn", true);
yield return new WaitForSeconds(1);
Transition.GetComponent<Animator>().SetBool("FadeIn", false);
Transition.SetActive(false);
}
public IEnumerator FadeOut()
{
Transition.SetActive(true);
Transition.GetComponent<Animator>().SetBool("FadeOut", true);
yield return new WaitForSeconds(1);
Transition.GetComponent<Animator>().SetBool("FadeOut", false);
}
}