Unity Scene is not loading - c#

I have a button in my Unity2D game that the user can click and the scene will start over. On my button, I have a script attached with this code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Restart : MonoBehaviour
{
public void PlayAgain()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Debug.Log("Play Again Works");
}
}
I know that the button is working and the Play Again function is working because the Debug.Log is working. However, when I click the button the scene doesn't restart. When I click the button, in the Unity hierarchy, next to the scene name it very briefly shows "(not loaded)". My scene is showing up in the Build settings. Does anyone know what could be causing this? Thanks for your help.

First check that you have added the scene in your build settings. If that is not your problem try following code.
SceneManager.LoadScene (SceneManager.GetActiveScene().name);

Related

How can I change scene by pressing a key in unity

Now I working on project like black mirror banderanatch. I create a movie that player has to interact with by pressing a specific key on keyboard to change to another scene. I dont know how to code it, I don't need a box or anything to click because in video I will say what button to press to go on another scene. I've tried put the video in UI in each scene but its does not work
some of the code I'm using
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeSceneWithButton : MonoBehaviour
{
public void LoadScene(string sceneName1)
{
SceneManager.LoadScene(sceneName1);
}
if{keyboard.write() a;
LoadScene(sceneName1)}
}
what should I add to make it change scene with specific button
ps.first time using unity any advice please teach me
You need to check the input in the Update function, like this:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ChangeSceneWithButton : MonoBehaviour
{
[SerializeField] private string _sceneName;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) SceneManager.LoadScene(_sceneName);
}
}

Quit Application on Escape button pressed

I'm currently making a simple 2D Pong game in Unity for a school project, and I have this code attached to my camera so that when the exit key is pressed it will close it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EscapeScript : MonoBehaviour
{
void Update()
{
if (Input.GetKey("escape"))
{
Application.Quit();
}
}
}
However whenever I'm in it, pressing Escape does absolutely nothing. I have tried variations such as "GetKeyDown" and "KeyCode.Escape" but they don't seem to work either
Never mind, I figured it out. It was because I was in the editor, and Application.Quit() doesn't work while in the editor. I needed to use UnityEditor.EditorApplication.isPlaying = false;

How can I make another scene after scanning the image target

Good day everyone,
how can I make another scene after scanning the image target? So far I can only do the buttons which are the basics and am currently working with c#, visual studio, unity and vuforia.Iv'e done the main menu screen which has the buttons scan and exit. Whenever you click the scan button it will automatically go to the AR camera and my question is how can I make a AR camera which you can scan an image and go to the another scene. No animation or whatsoever, just scan the image and go to another scene. Does someone has an idea of how to make it happen?
so far this is what iv'e coded:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
public void System()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
}
public void Back()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex -1);
}
public void Exit()
{
Debug.Log("You have exited the app");
Application.Quit();
}
}
In your case you can just create a gameobject as a child of your image target, attach a script, lets say sceneloader.cs and put in the start() method your load-scene-code....but this are total basics. Did you even read the documentation how image targets work?

Unity scene switching takes too much time

I have created a game with a main menu in a scene and I have a second scene that has the actual game in it. When the user taps the play button on main menu scene it loads the actual game scene but the problem is that it takes too much time. What should i do?
Here is the code I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
public void PlayGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void QuitGame()
{
Debug.Log("Ho gaya....! Mera Ho gya");
Application.Quit();
}
}
You can use LoadSceneAsync to load the level in advance, but not activate it, you get a callback when the load is done, and than you can simply activate the scene when the user presses the button which should be instant
Alternatively to the above, you could also wait until said button press to call SceneManager.LoadSceneAsync, but display a curtain (aka loading screen) while the actual game scene loads. Coroutines are you friend here, as you can wait for the completion of an AsyncOperation by yield returning it from a Coroutine IE
var curtain = Instantiate(CurtainPrefab);
DontDestoryOnLoad(curtain);
yield return SceneManager.LoadSceneAsync(gameSceneIndex);
// Don't allow unload to clean up this object or it'll stop the coroutine before destroying the curtain!
DontDestroyOnLoad(gameObject);
yield return SceneManager.UnloadSceneAsync(mainMenuSceneIndex);
Destroy(curtain);
// Do this last or the Coroutine will stop short
Destroy(gameObject);

Unity 5.4 C# Animation Play/Stop

I'm trying to play/stop animations with C# codes but they didn't worked, Javascript code work (legacy mode). I hope someone know where is problem.
C#:
using UnityEngine;
using System.Collections;
public class PlayStop : MonoBehaviour
{
public GameObject Cube;
void PS()
{
GetComponent<Animation>().Play();
}
}
JS:
var Cube : GameObject;
Cube.GetComponent.<Animation>().Play();
C# Screenshot
JS Screenshot
The problem is that mentioned C# and JS code you wrote are not equivalent.
In C# code you call GetComponent<Animation> to search in gameObject to which PlayStop script is attached to, while in JS script you are searching with GetComponent<Animation> in Cube gameObject you dragged from inspector.
You should try:
using UnityEngine;
using System.Collections;
public class PlayStop : MonoBehaviour
{
public GameObject Cube;
void PS()
{
//Will search in Cube game object for component of Animation type
Cube.GetComponent<Animation>().Play();
}
}
To run code inside PS() on application run, rename it to Start() or move code from inside of it to new method and name it Start().
To run PS() on button click, click + in OnClick section of Button in Inspector, drag your PlayStop script to box on the left and choose the function you want to call on click from dropdown, like this:

Categories

Resources