Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I need help with scene switching

Discussion in 'Scripting' started by julianisthebest12, Jan 7, 2021.

  1. julianisthebest12

    julianisthebest12

    Joined:
    Oct 23, 2020
    Posts:
    6
    Hey guys! Me and a couple friends are working on a game where you are able to switch between 2 different game scenes for a short period of time. We want you to be able to press "Q" and switch into a different, and after 10 seconds return back to the original scene using a coroutine. Also, if it's possible, is there any way that if the different scenes are built the game with the same coordinates, that you could put the player in the exact coordinates they left off in the other scene when they switch back. Here is my code.
    ///
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class WARP : MonoBehaviour
    {
    // Start is called before the first frame update


    // Update is called once per frame
    void Update()
    {

    if (Input.GetKey(KeyCode.Q))

    {
    StartCoroutine(Mindshift());
    SceneManager.LoadScene("GameScene2");
    IEnumerator Mindshift()
    {
    yield return new WaitForSeconds(10);
    SceneManager.LoadScene("GameScene");
    }

    }
    }
    ///

    Hopefully someone understands this, the script isn't throwing any error but still doesn't work when I press "Q". Have a good one y'all.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
  3. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    Can you please illustrate what specifically doesn't work? Is it that there is no effect at all or something else?
     
  4. julianisthebest12

    julianisthebest12

    Joined:
    Oct 23, 2020
    Posts:
    6
    Yeah, nothing happens at all
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Only one place to start with that! To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  6. julianisthebest12

    julianisthebest12

    Joined:
    Oct 23, 2020
    Posts:
    6
    Will do, Thanks for the suggestion.
     
  7. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Use GetKeyDown(), GetKey() fires repeatedly. Though not sure in this case if it's switching scenes...
    Are both scenes in the Build settings?
    To remember position, use a static Vector3 to store the player position, and update it right before switching to the next scene. Will take some fiddling to reload it etc.