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
Related
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.
Hi I tried a simple program in unity this is the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript1 : MonoBehaviour
{
// Start is called before the first frame update
public void Setr()
{
Debug.Log("I am alive!");
}
}
and then I add object select the event drag my script and then I cant choose the Setr function
I'm guessing you are trying to add this to some sort of OnClick() or other event. Make sure you add this script to a gameObject and drag the gameobject not the script into the event in the editor. From there you should be able to select your function.
I'm a beginner in Unity and C#. I tried building a basic game where the GameObject's presence in the scene is toggled when a key is pressed. The GameObject does disappear when I press "Space", but it never appears when I press the same key again. The following is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
private GameObject aj;
private bool flag = true;
void Start () {
aj = GameObject.FindGameObjectWithTag("robo");
}
void Update () {
if (Input.GetKey(KeyCode.Space))
{
flag = !flag;
aj.SetActive(flag);
}
}
}
Please let me know where the error lies.
Posting this as answer for future viewers:
The main reason that the gameobject doesn't reappear is because when you turn off the object, the script attached to it is disabled as well. In order to fix this, you must have the script to disable it, on a different object. I usually use a manager object which is just an empty gameobject with scripts on it.
A secondary issue in your code is that you're using if(Input.GetKey(KeyCode.Space)) in update which will try to change it every frame. Change it to if(Input.GetKeyDown(KeyCode.Space)) to have it only be true on the first frame pressed.
From a first view the code seems correctly, the only problem that I could figure it out is that you disable the object where the script is placed or his parent, and so the script won't be executed the second time.
P.s. I recommend you to not use that "flag" variable, instead would be more precise to use the GameObject.activeSelf attribute.
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.
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?