How can I change scene by pressing a key in unity - c#

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);
}
}

Related

When camera position set to object's, camera doesn't work

I have a camera with all of the default settings with the following C# script below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera1 : MonoBehaviour
{
// Update is called once per frame
void Update()
{
transform.position = GameObject.Find("Thing").transform.position;
}
}
The project runs normally (The player still moves), but I get this
Under the culling mask you need to create a layer for the gameObject your camera is set to follow, otherwise it will not see through it. You can also set its transform position just to be above/in front of it.

Unity Scene is not loading

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);

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 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