Executing a Function Between Script Files - c#

Tell me please. I have 2 scripts that hang on different Unity objects. The first script is for clicking a button. The second one is for executing the function.
How can I execute the AddItem function if the button is pressed?
Script 1 (button click):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
Script 2 (Adding Items):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InventoryManager : MonoBehaviour
{
public void AddItem(ItemScriptableObject _item, int _amount)
{
foreach(InventorySlot slot in slots)
{
if (slot.item == _item)
{
slot.amount += _amount;
return;
}
}
foreach(InventorySlot slot in slots)
{
//print(_item+" "+_amount);
if (slot.isEmpty == true)
{
slot.item = _item;
slot.amount = _amount;
slot.isEmpty = false;
slot.SetIcon(_item.icon);
return;
}
}
}

If you dont have a variable on the second script, of the first script, you should do that by using:
GameObject go;
go.GetComponent<InventoryManager>();
try changing the first script to something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
void Update()
{
if (isClick)
{
Debug.Log("do something")
}
}
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
This way you can modify the variable: is Click easily.

Related

Assets\EndTriger.cs(6,1): error CS8803: Top-level statements must precede namespace and type declarations

So i made a starter game from brackeys and i get 3 errors please help me
Script:GameManager
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public bool gameHasEnded = false;
public float restartDelay = 1f;
public void CompleteLevel ()
{
Debug.Log("LEVEL COMPLETE");
}
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("Restart", restartDelay);
}
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
script:EndTriger
using UnityEngine;
public class EndTriger : MonoBehaviour
public GameManager gameManager;
{
void OnTriggerEnter()
{
gameManager.CompleteLevel();
}
}
EndTriger:
using UnityEngine;
public class EndTriger : MonoBehaviour
{
public GameManager gameManager;
void OnTriggerEnter()
{
gameManager.CompleteLevel();
}
}

Unity C# Input for continuous events?

Having some major trouble trying to use the updated Unity Input system, specifically with the Hold interaction. I'd like to continuously run code the code only during the performed phase, and have it do so continuously until the key being pressed is let up. Something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerInput playerInput;
private PlayerInputActions playerInputActions;
void Awake()
{
playerInput = GetComponent<PlayerInput>();
playerInputActions = new PlayerInputActions();
playerInputActions.Player.Forward.performed += Forward;
playerInputActions.Enable();
}
void onEnable() {
playerInputActions.Enable();
}
void onDisable() {
playerInputActions.Disable();
}
public void Forward(InputAction.CallbackContext context) {
while (context.performed) {
Debug.Log("Hello");
if (context.canceled) {
break;}}
}}
How do I reprogram this in order to function as intended?
Something like this should work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
private PlayerInput playerInput;
private PlayerInputActions playerInputActions;
private bool buttonPressed;
void Awake()
{
playerInput = GetComponent<PlayerInput>();
playerInputActions = new PlayerInputActions();
playerInputActions.Player.Forward.performed += holdCallback;
playerInputActions.Player.Forward.canceled += releaseCallback;
playerInputActions.Enable();
buttonPressed = false;
}
void onEnable() {
playerInputActions.Enable();
}
void onDisable() {
playerInputActions.Disable();
}
void Update(){
if(buttonPressed)
Forward();
}
void holdCallback(InputAction.CallbackContext context){
buttonPressed = true;
}
void releaseCallback(InputAction.CallbackContext context){
buttonPressed = false;
}
void Forward() {
Debug.Log("Hello");
}
}

Button and PlayerPrefs

i have a code i want when i click button not set active (with PlayerPrefs) but problem when i run game a button not set active before i click it (button)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fcfg : MonoBehaviour
{
public GameObject butt;
void Start()
{
if (PlayerPrefs.GetInt("ButtonActivate") == 0)
{
butt.gameObject.SetActive(false);
}
else if (PlayerPrefs.GetInt("ButtonActivate") == 1)
{
butt.gameObject.SetActive(true);
}
}
void Update()
{
}
public void whenbuttonclick()
{
this.gameObject.SetActive(false);
PlayerPrefs.SetInt("ButtonActivate", 0);
}
}

Touch event does not do any action on the Unity c # smartphone

I have this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Audio;
using UnityEngine.EventSystems;
public class start : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public GameObject musica;
public bool pulsado;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnPointerDown(PointerEventData eventData)
{
pulsado = true;
audio();
SceneManager.LoadScene("pajaro");
}
public void OnPointerUp(PointerEventData eventData)
{
pulsado = false;
}
void audio()
{
musica.SetActive(true);
}
}
When I export it to Android, touch does not work, but if it works in unity
You're using OnPointerDown, this is specific to the mouse, this event does not fire for Touch input (touch input does not have a concept of "up" and "down").
In order to have touch input, you have to use Input.touches during the Update() loop.

How to enable/disable scripts in 1 gameObject?

I have gameObject MainCamera and there is script LookAtCamera as my main view option and I have script MouseLook that I wanna make secondary with right-click.
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
LookAtCamera.enabled = false;
MouseLook.enabled = true;
}
}
}
How to declare script as a public component of MainCamera? I just have one of them enabled and switch between with mouse right click.
You've got most of it done. So, declare the scripts as a public variable like below, and then assign them on the inspector:
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
public LookAtCamera lookAtScript;
public MouseLook mouseLookScript;
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
lookAtScript.enabled = false;
mouseLookScript.enabled = true;
}
}
}
Thx :) I didn't know how to declare script. I also added else to go back to default camera setup.
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
public LookAtCamera lookAtScript;
public MouseLook mouseLookScript;
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
lookAtScript.enabled = false;
mouseLookScript.enabled = true;
} else {
mouseLookScript.enabled = false;
lookAtScript.enabled = true;
}
}
}

Categories