Spigot/Bukkit Plugin: How to Me Check Item in Player Right Hand? - Minecraft - plugins

Exaple of answer:
if(player has clicked && item in player right hand is stick) {
// Do some action
}
Thanks in advance

You can use PlayerInteractEvent.
#EventHandler
public void onPlayerUse(PlayerInteractEvent event){
Player p = event.getPlayer();
if(p.getItemInHand().getType() == Material.BLAZE_ROD){
// Code goes here
}
}
Also don't forget to register your listener in onEnable method.

Related

AudioSource attached button doesnt work in another scene

public AudioSource menumusic;
public Button SoundButton;
public Sprite Soundoff;
public Sprite SoundOn;
bool isClicked = true;
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
SoundButton.image.sprite = SoundOn;
}
// Start is called before the first frame update
void Start()
{
menumusic = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
SoundButton.onClick.AddListener(BtnOnClick);
}
void BtnOnClick()
{
changeState();
}
private void changeState()
{
isClicked = !isClicked;
if(isClicked)
{
SoundButton.image.sprite = SoundOn;
menumusic.Play();
}
else
{
SoundButton.image.sprite = Soundoff;
menumusic.Stop();
}
}
I have script like this And I am using on menu scene but when I switch to game scene with other button , this script works in there to except that he can not find soundbutton , how can I link to sound button from another scene should I create same to game scene too ? , sorry I am new on unity.
It looks like you're setting the reference to your "SoundButton" object in the inspector. When you change scenes, you'll need to update the reference on this script to refer to the new button.
There are many ways to do this. You could just tag the soundButtons with "soundButton".
Then, wherever you're loading a new scene, update the reference by searching for the tag.
SoundButton = GameObject.FindObjectWithTag("soundButton").GetComponent<Button>();
It would also be possible to have a SoundButton script which you place on every sound button. This could then reference a static SoundManager object, and tell it to toggle the sound on or off when clicked. This is probably a cleaner way to do it.
Also note: You're adding a listener to the soundButton in Update() (i.e. every frame). This is unnecessary. Add the listener one time in Start(), or when you change Scenes, and it will trigger the event whenever the button is clicked. There is no need to use Update() for this process.
If it still isn't working, you can also check the variable before using it, and get the reference again if it's null:
if(soundButton == null)
{
SoundButton = GameObject.FindObjectWithTag("soundButton").GetComponent<Button>();
}

How can I solve the Unity GameObject Problem? Solved

When the Continue Ads gameobject is closed, the Game over button works, but when the Continue Ads is turned on, the Game over button does not work. I cannot understand why I make a mistake?
!Me fixed the problem, the problem is that I noticed that it is related to the continue ads dimension, thank you for your time.
void Update()
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
//Debug.Log("No internet access");
Destroy(GameObject.FindWithTag("ContinuePanel"));
}
else
{
//Debug.Log("internet connection");
if (tag == "Skull")
{
ContinueAds();
}
}
}
public void ContinueAds()
{
gameObject.AddComponent<AdsScript1>();
gameoverPanel.SetActive(false);
PausedGamePanel();
}
public void GameoverButton()
{
gameoverPanel.SetActive(false);
SceneManager.LoadScene(0);
}
Your continue ads object is in gameover panel. When you set gameoverPanel.SetActive(false) you also do the same on the child objects.

Duplicated objects are not working 'per instance' when I intended them to do [duplicate]

This question already has answers here:
How to detect click/touch events on UI and GameObjects
(4 answers)
Closed 5 years ago.
using UnityEngine;
public class LinkEnd : MonoBehaviour
{
public GameObject linkTarget;
private PointEffector2D effector;
private CircleCollider2D contact;
private AimSystem aimer;
private float distFromLink = .2f;
public bool connected;
private void Start()
{
aimer = GetComponent<AimSystem>();
}
private void Update()
{
SyncPosition();
ReactToInput();
}
public void ConnectLinkEnd(Rigidbody2D endRB)
{
HingeJoint2D joint = GetComponent<HingeJoint2D>();
if (GetComponent<HingeJoint2D>() == null)
{
joint = gameObject.AddComponent<HingeJoint2D>();
}
joint.autoConfigureConnectedAnchor = false;
joint.connectedBody = endRB;
joint.anchor = Vector2.zero;
joint.connectedAnchor = new Vector2(0f, -distFromLink);
}
private void SyncPosition()
{
if (linkTarget != null)
{
if (Vector2.Distance(transform.position, contact.transform.position) <= 0.1f)
{
connected = true;
effector.enabled = false;
contact.usedByEffector = false;
}
}
if (connected)
{
GetComponent<Rigidbody2D>().isKinematic = true;
GetComponent<Rigidbody2D>().position = linkTarget.transform.position;
}
else
GetComponent<Rigidbody2D>().isKinematic = false;
}
private void ReactToInput()
{
if (Input.GetKeyUp(KeyCode.Mouse0) || Input.GetKey(KeyCode.Mouse1))
{
connected = false;
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.GetComponent<PointEffector2D>() != null)
{
connected = true;
linkTarget = collision.gameObject;
effector = linkTarget.GetComponent<PointEffector2D>();
contact = linkTarget.GetComponent<CircleCollider2D>();
}
}
public void OnTriggerExit2D(Collider2D collision)
{
connected = false;
contact.usedByEffector = true;
effector.enabled = true;
}
}
This is an object that pins its position to another mobile object on collision, and it's supposed to stay that way until it's 'detached' by player action.
It's working almost fine, but it's not working 'per instance.'
Whether this object is a prefab or not, ReactToInput() is affecting all instances of it unlike how I wanted.
I'm missing some per instance specification here and I'm not seeing where.
Any suggestion will help and be appreciated!
++ The method ReactToInput() is triggered by key inputs. I wanted this method to be called when Player's attack 'method' happens which are bound to those key inputs, but I did what I've done only because I couldn't find an elegant way to execute it otherwise, and am really hoping there's a better way rather than using tags or GetComponent to specific object since it's supossed to affect other objects as well.
These methods are what you are looking for.
/// <summary>
/// OnMouseDown is called when the user has pressed the mouse button while
/// over the GUIElement or Collider.
/// </summary>
void OnMouseDown()
{
Debug.Log("Hi!");
}
/// <summary>
/// OnMouseUp is called when the user has released the mouse button.
/// </summary>
void OnMouseUp()
{
Debug.Log("Bye!");
}
MonoBehaviour provides many event callbacks other than Update(), and these two are some of them. The full list of event callbacks you can use for MonoBehaviour is explained in the official Monobehaviour page.
There are two methods for OnMouseUp():
OnMouseUp is called when the user has released the mouse button.
OnMouseUpAsButton is only called when the mouse is released over the
same GUIElement or Collider as it was pressed.
The GameObject your script is attached to is requires to have GUIElement or Collider as described in the manual page to use these functions.
If you do not want to use these methods, you could alternatively write your own custom InputModule, raycast to the mouse position on the screen to find which object is clicked, and send MouseButtonDown() event to a clicked GameObject.
I had to implement a custom input module to do this plus a couple of other stuff and I assure you writing custom InputModules is a headache.
EDIT:
If many different classes need to be notified when something happens, and who listens to such cases is unknown, event is a good option.
If you are using events, each event listener class such as LinkEnd is responsible to register and remove itself to such event.
Below is an example of how you could achieve this behaviour:
class Player
{
public delegate void OnSkillAFiredListener(object obj, SkillAFiredEventArgs args);
public static event OnSkillAFiredListener SkillAPressed = delegate { };
// ...
}
class LinkEnd
{
void OnEnable()
{
Player.SkillAPressed += WhatToDoWhenSkillAFired;
}
void OnDisable()
{
Player.SkillAPressed -= WhatToDoWhenSkillAFired;
}
void OnDestroy()
{
Player.SkillAPressed -= WhatToDoWhenSkillAFired;
}
public void WhatToDoWhenSkillAFired(object obj, SkillAFiredEventArgs args)
{
// get info from args
float someInfo = args.someInfo
// do something..
Bark();
}
// ...
}
It's necessary to deregister from the event in both OnDisable() and OnDestory() to avoid memory leaks (some claim such memory leaks are very minor).
Look for Observer and Publisher/Subscriber pattern to learn more about these approaches. It may not be very related to your case, but the Mediator Pattern is something that's often compared with the Observer Pattern so you might be interested to check it as well.

Virtual Buttons get Pressed on their own

I have made an AR app in Unity using Vuforia. I have some Virtual Buttons in the scene. The problem I am facing is that the virtual button is getting pressed on their own. I am not able to figure out what I am doing wrong. All the buttons are in a layer named "Buttons". Here is the virtual Button handler script:
private List<GameObject> vbtns= new List<GameObject>();
// Use this for initialization
void Start () {
var goArray = FindObjectsOfType<GameObject>();
for(var i=0; i<goArray.Length; i++)
{
if(goArray[i].GetComponent<VirtualButtonBehaviour>())
{
//Debug.Log("The game component is " + goArray[i]);
goArray[i].GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
vbtns.Add(goArray[i]);
}
}
}
// Update is called once per frame
void Update () {
}
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Button Pressed.");
Debug.Log("The name of the vbutton is "+ vb.VirtualButtonName);
SceneManager.LoadScene("scene_"+vb.VirtualButtonName,LoadSceneMode.Single);
}
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Button Released");
}
Please suggest what could be the reason for this. I have attached the screenshot of the game view below. Thanks in advance.

Instead of LoadLevel, how do i use SceneManager

I super new in coding and I couldn't find the right answer on the web. All I want is just change scene in my game. I've already had the buttons etc. But i can't choose the "menu" script from the On click function menus.
All answer is welcome!
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class Menu : MonoBehaviour {
public void ChangeScene(string sceneName)
{
SceneManager.LoadScene("sceneName");
}
}
Several problems here. First, the function "ChangeScene"
public void ChangeScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
Then, you may have a problem in your scripts since you can't add any listener to the OnClick delegate. Fix every problem thrown by the console. A strange thing is that the name of the Menu script does not appear in the Menu component. Make sure the filename is "Menu.cs"
Finally, drag & drop the button into the field under "Runtime Only" and select "Menu > ChangeScene"
You can't just plug in the Menu script to the left slot.
You have to attache Menu to a GameObject then plug that GameObject to the left slot.You will then be able to chose which script and function to send the event to on the right.
The Image below shows the wrong way to do this(This is how you are currently doing it):
The is the correct way to do it:
You can also do this from code:
public class Menu : MonoBehaviour
{
public Button playButton;
void Start()
{
//Add Button Event
playButton.onClick.AddListener(() => buttonCallBack(playButton));
}
public void ChangeScene(string sceneName)
{
SceneManager.LoadScene("sceneName");
}
private void buttonCallBack(Button buttonPressed)
{
if (buttonPressed == playButton)
{
ChangeScene("myscene");
}
}
}