I made a couple of prefab buttons. When I click each one of them they go to the same scene, but I that scene to have different information according to which button I clicked. How can know which was the button I clicked? The function goArtistDetail is the one being called by every button.
I have this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonScriptTwo : MonoBehaviour
{
// Use this for initialization
void Start ()
{
Debug.Log("ButtonScriptTwo");
Debug.Log("counter: " + GlobalState.counter);
}
public void goArtistDetail(Button button)
{
GlobalState.counter++;
Application.LoadLevel("ArtistDetail");
}
}
Prefab of the Button
is there any reason why you are trying to use the same method for both calls other than that the same scene is being loaded? If not why not just have a different method for each buttons onClick and set a PlayerPref.
public void ButtonClick_1()
{
GoArtistDetail(1);
}
public void ButtonClick_2()
{
GoArtistDetail(2);
}
// C# Methods start with a Captial
private void GoArtistDetail(int refererBtn)
{
GlobalState.counter++;
PlayerPrefs.SetInt("Referer Button", refererBtn)
Application.LoadLevel("ArtistDetail");
}
An then in the ArtistDetail scene
private void Start()
{
int refererBtn = 1; //Default value
if(PlayerPrefs.HasKey("Referer Button"))
{
refererBtn = PlayerPrefs.GetInt("Referer Button");
PlayerPrefs.DeleteKey("Referer Button");// Remove it if it is use once
}
//Evaluate refererBtn to show what info is relevant
}
Related
Trying to combine these two:
https://answers.unity.com/questions/1171111/hideunhide-game-object.html
Show/ hide Unity 3D elements
I got this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Modify_Menu_Button_Handler : MonoBehaviour
{
void Start()
{
GameObject[] buttons_in_create_menu;
buttons_in_create_menu = GameObject.FindGameObjectsWithTag("CreateMenu");
}
public void ChangeMenu()
{
foreach (GameObject button in buttons_in_create_menu)
{
button.SetActive(false);
}
}
}
But, the foreach line is red-underlined, saying the buttons_in_create_menu doesn't exist in the current context.
Coming from Javascript, I'm not sure how scope works in C# and especially within Unity's game loop. Thought the Start() function would be called upon the scene loading, based on the Unity docs: link
You must define the variable globally in the class.
Like this:
public class Modify_Menu_Button_Handler : MonoBehaviour
{
public GameObject[] buttons_in_create_menu;
void Start()
{
buttons_in_create_menu = GameObject.FindGameObjectsWithTag("CreateMenu");
}
public void ChangeMenu()
{
foreach (GameObject button in buttons_in_create_menu)
{
button.SetActive(false);
}
}
}
I've been trying to make the options in my game being selected by a keyboard input. I can highlight them, but I don't know how to make Unity identify which of the buttons is beeing pressed in order to do a certain action, it's giving me the NullReferenceException in line 28 of my code. The cript in question is the BattleSystem script, it's attached to the event system, the battleFirstButton is the fight button and the enterKey is "Z".
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class BattleSystem : MonoBehaviour
{
public GameObject battleFirstButton;
public KeyCode enterKey;
Button selectedButton;
// Start is called before the first frame update
void Start()
{
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(battleFirstButton);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(enterKey))
{
selectedButton.onClick.Invoke();
}
}
public void SetSelectedButton()
{
selectedButton = GetComponent<Button>();
}
public void Fight()
{
print("Fight option submitted");
}
public void Act()
{
print("Act option submitted");
}
public void Item()
{
print("Item option submitted");
}
public void Mercy()
{
print("Get dunked o-, I mean, Mercy option selected");
}
}
selectedButton is a private variable potentially never set to anything, so it's null. Make sure it's set to something before you access it.
Probably the simplest fix to the way you have it set up is:
void Update()
{
if (Input.GetKeyDown(enterKey))
{
// Gets the focused button
selectedButton = EventSystem.current.currentSelectedGameObject.GetComponent<Button>();
if (selectedButton != null)
{
selectedButton.onClick.Invoke();
}
}
In the editor i did in the menu: GameObject > UI > Button
Now i have in the Hierarchy a Canvas with a a Button.
Now i want when i'm running the game that it will not show the button, and only when i press the escape key it will show the button.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NodesGenerator : MonoBehaviour {
public Button btnGenerate;
private void Start()
{
Button btn = btnGenerate.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Debug.Log("You have clicked the button!");
}
I want that when i press the escape key the btn will show and escape again will not show. The default state when running the game is not showing the button.
Imagining that by "hiding" you mean that you deactivate the object holding your button, you need to check in an Update function if you hit the Escape key or not. If you did hit it, you just need to reverse the active state of your button, and you're done.
As a side note, in your Start function, you do not need to get your Button component again, as you already have it referenced in your btnGenerate variable. So you can directly add the listener to your btnGenerate variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NodesGenerator : MonoBehaviour {
public Button btnGenerate;
private void Start()
{
btnGenerate.onClick.AddListener(TaskOnClick);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
btnGenerate.gameObject.SetActive(!btnGenerate.gameObject.activeSelf);
}
}
void TaskOnClick()
{
Debug.Log("You have clicked the button!");
}
}
Hello i made a canvas in unity.
when the player preses the Esc button a canvas opens
In the canvas i made a button when the player clicks on the button the
Canvas need to close but nothing happens when i press the button.
Can somebody help me with this problem?
The Canvas
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
private Canvas PlayerPauseMenu;
// Use this for initialization
void Start()
{
PlayerPauseMenu = GetComponentInChildren<Canvas>();
}
// Update is called once per frame
void FixedUpdate()
{
HandleInput();
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
PlayerPauseMenu.enabled = true;
}
}
public void CloseCanvas()
{
PlayerPauseMenu.enabled = false;
}
}
There's a void in the script, which reloads Canvas. The Canvas contains the Button object. I attach the reload void with the script.
using UnityEngine;
using UnityEngine.UI;
public class Trashscript : MonoBehaviour {
public GameObject Temp;
private GameObject Active;
int Counter;
void Start()
{
Menu();
}
void Menu()
{
Counter++;
if (Active != null) Destroy(Active);
Active = Instantiate(Temp);
GameObject.Find("Text").GetComponent<Text>().text = Counter.ToString();
GameObject.Find("Btn").GetComponent<Button>().onClick.AddListener(Menu);
}
}
First call (from Start) works fine the "Text" element shows "1". After clicking the "Btn" Canvas reloads, but there isn't any "Text", and "Btn" doesn't work (onClick event do nothing).
Help.
I can't tell what your code is doing but each time the Button is clicked, Menu function is called which leads to GetComponent<Button>().onClick.AddListener(Menu) being called again.
You have to move GetComponent<Button>().onClick.AddListener(Menu) out of the Munu function and into the Start() function. You also have to un-register to the Button event in the OnDisable() function.
Below is what your code should look like:
Button button;
void Start()
{
button = GameObject.Find("Btn").GetComponent<Button>();
button.onClick.AddListener(Menu);
}
void OnDisable()
{
button.onClick.RemoveListener(Menu);
}
void Menu()
{
///Put Your Button click code here
}
I solved my problem with changing the GameObject.Find("") to Active.transform.Find("").