SceneManager.LoadScene not loading scenes - c#

using UnityEngine;
using UnityEngine.SceneManagement;
public class Buttons : MonoBehaviour
{
public void loadLevel()
{
SceneManager.LoadScene("Level");
}
}
This is the code I am using, loadLevel() is supposed to load the scene Level when a button is clicked, this should be simple and I am not sure what is going wrong.
Thanks in advance!

I guess you haven't added the scene "Level" in Build Settings (menu "File"-"Build Settings"). If it isn't in the build it can't be opened by LoadScene.
To add a scene you can either open it in the editor and then click on the "Add Open Scenes" button, or you can simply drag-and-drop the scene file from the Project-window to the "Scenes In Build".

Related

my button doesnt trigger as should and instead gives two errors (before and after click)

hello I have a button plus a box which alpha is 0 from the start, the button works but the box doesn't appear. In the code the I did use 'getcontents' but it seems it didn't work, the button executes a debug.log so that's how I know that is working, the errors I get before the button click is;
before button click
and the errors after button click (including the debug.log)
after button click
it says i haven't attached my 'Menuee' although i have (yes i know i spelt it like that)
show here;
attached Menuee
and i have attached my Ibutton (the button)
my code is below;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class startbutton : MonoBehaviour {
public Button Ibutton;
public CanvasGroup Menuee;
public void Start () {
Ibutton.onClick.AddListener(TaskOnClick);
}
public void TaskOnClick(){
Debug.Log ("yes");
Menuee.interactable= true;
Menuee.alpha = 1;
}
}
In the screen shot you don't seem to have anything assigned to Button Ibutton.
Another reason for such errors could be that you might have assigned the startbutton script to another game object somewhere and that instance of the script doesn't have the properties assigned.
You didn't add a button on your public Button Ibutton;
You might also some how delete EventManager that comes with canvas you created on hierarch window.

How to make different buttons in one scene change to their own specified scene uppon click?

I've tried different tutorials but the scenes won't change for any of the ones in that scene. I have a scene that goes before this one and it only has one button that changes scenes and it works, but not the buttons in my scene with multiple buttons. And I've added all the scenes in the build settings.
This is the code I used for the button in the scene that has only one
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour
{
public void playGame()
{
SceneManager. LoadScene(Scene Manager.GetActiveScene().buildIndex + 1);
}
}
Then I draged the script to Canvas> went to button> added on click> draged canvas to spot under Runtime Only > NewBehaviourScript > playGame ()
Try this script:
public void playGame(int levelIndex)
{
SceneManager.LoadScene(levelIndex);
}
and reassign your script to your buttons. after each assignment, you can enter your level index on your own button.

Unity - opening and closing animation with key binding

I'm making an inventory menu that will be accessed via a key binding (I). When the keybinding is clicked then play the animation which brings in the menu, if the keybind is clicked again then it should close the menu. Not sure where I'm going wrong here.
I've attached the animation controller to the UI.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShowInventory : MonoBehaviour {
public Animator animator;
// Update is called once per frame
void Update() {
if (Input.GetKeyDown(KeyCode.I)) {
animator.SetBool("isOpen", true);
}
}
}
As well as the code, you must also make sure your state machine in the Animator Controller asset is set up properly too. Check out this tutorial if you haven't already: https://unity3d.com/learn/tutorials/topics/animation/animator-controller

Unity Scripting Main Menu Not Working? Don't Know Why?

I made a main menu in unity so now I'm down to the scripting. I have tried the mouseup / mousedown functions but nothing is wrong with my code but it won't work period no debug logs not errors just plain nothing.
Here's my C# Script to change levels.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour {
public bool Start;
public bool Quit;
void OnMouseUp(){
if(Start)
{
SceneManager.LoadScene(1);
}
if (Quit)
{
Application.Quit();
}
}
}
This is very simple but I still don't see why it isn't working.
Try OnMouseDown
As an event needs to happen first. So the player presses down then upon release the mouse button is up, you need that first click. hope that helps.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
Make sure that you added the script to a gameobject inside your scene. Also make sure that Start or Quit are set to true.

How can I properly load my scene from another with a button click

I have 2 scenes, 'scene1' and 'scene2', first, i had to switch from scene1 to scene2, it worked perfectly by adding 'Load Level On Click' to the button in scene1 (see it here)
Now from scene2 to scene1, this time by clicking on an UI Button on scene2, i'm using OnClick() and a script called 'LoadScene' (see it here),
here is my simple script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class LoadScene : MonoBehaviour {
void Start()
{
}
void Update()
{
}
public void Load () {
SceneManager.LoadScene("scene1");
}
}
Now, the problem is when i click the button (in scene2) to switch to scene1, it works but i lose a lot of elements on the scene1 like buttons and images.
I don't really know where does the problem come from !! Is there something to add to my script so i can load my scene correctly?

Categories