C
C
Chipu2018-09-25 20:47:50
C++ / C#
Chipu, 2018-09-25 20:47:50

Unity, I created a button but it does not respond to a click, how to fix it?

I created a button
5baa74563c6ce637650785.png
Then I created a script for it:
(the button is on the Scroll VIew)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UpButtonHelper : MonoBehaviour
{

    public Text DamageText;
    public Text PriceText;

    public int Damage = 10;
    public int Price = 100;

    private GameHelper _gameHelper;
    // Use this for initialization
    void Start()
    {
        DamageText.text = Damage.ToString();
        PriceText.text = Price.ToString();
        _gameHelper = GameObject.FindObjectOfType<GameHelper>();
    }

    // Update is called once per frame
        void Update ()
  {
    
  }

    public void UpClick()
    {
        if (_gameHelper.PlayerGold >= Price)
        {
            _gameHelper.PlayerGold -= Price;
            _gameHelper.PlayerDamage += Damage;

            Destroy(gameObject);
        }
    }
}

But when you click nothing happens (button settings below)
5baa74b43b57a969050292.png
How can I fix this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2018-09-25
@Chipu

just to clarify. is the condition exactly fulfilled?
if (_gameHelper.PlayerGold >= Price)
tried to pull it out from under the scrollrect? almost certainly because of him,
and so many things can be. above the button is something with raycast target ticked.
can hang event triger on content and it intercepts all events for itself and does not start up "below"
so in an empty scene.
-create a scroll view
-add buttons.
-on content we add some Layout.
We get a scrolling list of buttons and each one works.
See how else yours differs from this minimum working set.
on all the screenshots provided, everything is generally correct and adequate.
(although the button's RectTransform is missing and what's on the Content)

F
FadeToBlack, 2018-09-26
@FadeToBlack

It also happens that you accidentally deleted the EventSystem scene element, which is responsible for UI events.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question