- Home /
The question is answered, right answer was accepted
Load different Scene on first load
Hello everyone! I have got two different scenes: "Normal" scene and "FirstStart" scene. I want to load "FirstStart" on first game startup and "Normal" on every after. (Sorry for english)
Answer by Desoxi · Nov 17, 2016 at 10:10 AM
Hey @Mpl2014, You dont have to create any text files to check whether or not your app is starting the first time. Just use PlayerPrefs for this and a third scene in which you check stuff like that. This third scene has to be the very first scene in the scene load order inside your build settings. Then you could write something like this and put it inside that start scene.
Assuming you have a scene_01 and scene_02 and a start_scene:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class StartScene : MonoBehaviour
{
// Use this for initialization
void Start()
{
if(PlayerPrefs.GetInt("FirstLaunch") == 0)
{
//First launch
PlayerPrefs.SetInt("FirstLaunch", 1);
SceneManager.LoadScene("scene_01");
}
else
{
//Load scene_02 if its not the first launch
SceneManager.LoadScene("scene_02");
}
}
}
would be the script to put into the start_scene.
Answer by Zodiarc · Nov 16, 2016 at 03:46 PM
You need a scene before those two. In this scene you check for the existence of a file (maybe a save file). If it exists, you proceed with the "Normal" scene. If it not exists, you create it and load the "First start" scene.
Great idea but I am still a begginer in scripting. I tried alot of methods in C# but they dont worked so... Did somebody know that script?
You can use the Directory and File classes in C#. Look here http://answers.unity3d.com/questions/1252963/how-to-check-if-game-was-started-once.html#answer-1252968
Answer by JeffHardddyyy · Nov 16, 2016 at 05:00 PM
Go to file, build Settings, then drag FirstScene to the top.
Or attack this C# script to a empty object or something
void Start() {
Application.LoadLevel("FirstScene");
}
I'm not home so I don't remember if it's with or without Quotation marks
This won't do, because it will force the "FirstScene" to load every time application is opened.
Answer by Gasimo · Nov 17, 2016 at 09:54 AM
If somebody have same question here is answer:
import System;
import System.IO;
var fileName = "MyFile.txt";
function Start()
{
if (File.Exists(fileName))
{
Application.LoadLevel("YourLevel2");
Debug.Log(fileName+" already exists.");
return;
}
Application.LoadLevel("YourLevel");
var sr = File.CreateText(fileName);
sr.WriteLine ("Your SaveFile Text");
sr.WriteLine ("You Can write ewerything",
1, 4.2);
sr.Close();
}
Follow this Question
Related Questions
Loading a saved Game 1 Answer
Editor Crash: Where is Scene Data kept? 3 Answers
Load data from file, if scene changed, fails to load location 0 Answers
how to seperate two visually distinct game modes 1 Answer
Loading Sene problem 2 Answers