Answer the question
In order to leave comments, you need to log in
Why is the onclick method in unity not working?
why when I put this script on UI button in unity the onclick method doesn't see the "buy" function
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using System.IO;
public class shop : MonoBehaviour
{
string datatext;
public void buy(int numberofclick)
{
datatext = File.ReadAllText("shopdata.txt");
numberofclick = int.Parse(datatext);
numberofclick = numberofclick * 2;
File.WriteAllText("shopdata.txt", numberofclick.ToString());
}
}
Answer the question
In order to leave comments, you need to log in
In OnClick, you need to add not the script itself, but the object on which this script is located.
Create a dummy in the hierarchy, drop the script there and add this dummy to the button
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question