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.
Related
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.
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 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
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 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?