S
S
saturnmoon2021-11-27 15:46:03
C++ / C#
saturnmoon, 2021-11-27 15:46:03

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

2 answer(s)
L
LittleBob, 2021-11-27
@saturnmoon

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

D
Denis Konoplyanikov, 2021-11-29
@DeKon

onclick may not work with the new inputmanager

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question