-1

I want to use this int in the two scripts: public int wood; I want the value of the int to be the same in both scripts.

This is my first script:

public class Tree : 
  
MonoBehaviour { public int wood;
    
     private void OnTriggerEnter2D(Collider2D collision)
     {
         wood += 1;
     }
    }

This is my second script which I want the wood to use in it:

public class Base : MonoBehaviour {

 private void OnTriggerEnter2D(Collider2D collision)
 {
 }
}

I want to decrease the wood value in OnTriggerEnter2D. How do I do that?

tripleee
  • 175,061
  • 34
  • 275
  • 318
sadra
  • 11
  • 2

2 Answers2

0

You can add the button component to your gameobject.(To get this component in code use Button btn = Gameobject.GetComponent<Button>();. To detect a click on the button you can use the onclick event the button has.

void Start(){
    btn.onClick.AddListener(ButtonClicked);
}
void ButtonClicked(){
    //code
}

You can also add the OnMouseDown and OnMouseUp functions to create a button(this would only require the collider component, and not the button component.

void OnMouseDown(){
    clicking = true;
}
void OnMouseUp(){
    clicking = false;
}
ChilliPenguin
  • 710
  • 7
  • 14
  • I wrote Debug.Log("HI") in the start void but it didnt say any hing in console it didnt work I wrote: { bool clicking=false; void OnMouseDown() { clicking = true; } void Update() { if (clicking == true) { Debug.Log("clicking"); } } – sadra Aug 16 '20 at 22:11
  • Did you have a collider attached to the gameobject? Both methods I said should be done individually. However, you also mentioned void Start did not work, is the script attached to a gameobject(that is in the scene). – ChilliPenguin Aug 16 '20 at 22:25
  • I added public new GameObject gameObject; but it didnt say any thing in cosole the other codes are the same – sadra Aug 17 '20 at 06:32
  • I used the other solution Button button; public new GameObject gameObject; void Update() { button.onClick.AddListener(onclick); } void onclick() { Destroy(gameObject); } but the object didnt got destroyer – sadra Aug 17 '20 at 06:41
  • First. You are creating a new gameobject called "gameObject". Usally gameObject would refer to the object the script is set to, however you have reset it with a new gameObject. Try using this.gameObject to destroy the button. Second, put the `button.onClick.AddListener(onclick);` in `void Start()`, or the button will keep adding a listener from the button to the void. Finally, try putting a debug.log in the onclick function to see if it is running. – ChilliPenguin Aug 17 '20 at 11:52
0

When a button is clicked it throws an event. So you need to register what you want to do when this event fires.

There are multiple ways, but one could be like:

buttonVariable.onClick.AddListener(OnButtonDoSomething);

Using lambda expresion will be:

button.onClick.AddListener(() => OnButtonDoSomething());

So you need to declare this on Start, OnEnable or Awake method in any of your scripts. And then have a Button reference so you can store your buttonVariableand finally declare your method OnButtonDoSomething.

To fit your question, you'll store a boolean variable called buttonGotClicked and in your new created method, set this boolean like:

private void OnButtonDoSomething()
{
    buttonGotClicked = true;
}

Then you can do if(buttonGotClicked)...

Lotan
  • 4,078
  • 1
  • 12
  • 30