Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

I Can't Figure Out How to Reload a Scene Without Reloading the Player

Discussion in 'Scripting' started by baccaking778, Aug 2, 2020.

  1. baccaking778

    baccaking778

    Joined:
    Jul 27, 2020
    Posts:
    3
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    public class RewindTime : MonoBehaviour
    {
    public GameObject playerPrefab;
    private Scene ThisScene;
    void Start()
    {
    ThisScene = SceneManager.GetActiveScene();

    }
    void Onstart()
    {
    var player = GameObject.FindGameObjectWithTag("Player");
    if (player == null)
    {
    player = Instantiate(playerPrefab);
    DontDestroyOnLoad(player);
    }
    }
    void Update()
    {
    {
    if (Input.GetKeyDown(KeyCode.R))
    SceneManager.LoadScene(ThisScene.name);
    }
    }
    }



    [/code]




    my goal is to reload the scene without reloading the player but the best i can do is create two player
    I cant figure out whats wrong with this please help
     
    Last edited: Aug 2, 2020
  2. baccaking778

    baccaking778

    Joined:
    Jul 27, 2020
    Posts:
    3
    i hate how the brackets are moved to the left
     
  3. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    16
    Hello

    There is no opening tag

    Christoph
     
  4. Elango

    Elango

    Joined:
    Jan 27, 2016
    Posts:
    90
    You don't call OnStart() so this code doesn't get executed.
    Also, why are you instantiating and marking the player undestroyable in the script not related to the player? If the player has already been created and is not marked as undestroyable, then this code will not work and the player will be destroyed.
    And Application.LoadLevel() is obsolete. Use SceneManager.LoadScene().
     
  5. baccaking778

    baccaking778

    Joined:
    Jul 27, 2020
    Posts:
    3
    its related to the player because my goal is to reset the entire scene but save eerything about the player and not delete it and i have edited the script to i think reflect your changes and it still is not working
     
  6. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    357
    Use code tags
     
  7. mgrekt

    mgrekt

    Joined:
    Jun 22, 2019
    Posts:
    52
    Code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/page-2#post-6140955
     
  8. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    43
    In your Onstart() method you have no else feature which means if there is a player active in the scene he will be Destroied when reloading the scene
     
unityunity