- Home /
Why click event doesn't work?
Hi Guys, I am trying to understand how delegates/events work. I have a simple scene with a box which green by default and should change a colour to red when I click a checkbox in Unity inspector (and then back to green if it is unchecked). Don't understand why it is not working?
This is my Events Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameEvents : MonoBehaviour
{
public static GameEvents current;
void Awake()
{
current = this;
}
public event Action<bool> OnCheckboxClicked;
public void CheckboxClicked(bool click)
{
if (OnCheckboxClicked != null)
{
OnCheckboxClicked(click);
}
}
}
This is Box Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxController : MonoBehaviour
{
void Start()
{
GetComponent<Renderer>().material.SetColor("_Color", Color.green);
GameEvents.current.OnCheckboxClicked += ChangeColor;
}
private void ChangeColor(bool click)
{
GetComponent<Renderer>().material.SetColor("_Color", Color.red);
}
}
And Game Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public bool klick;
private void OnBoolenOn(bool click)
{
GameEvents.current.CheckboxClicked(click);
}
}
Comment
Your answer
Follow this Question
Related Questions
Problems with GameManagers 3 Answers
Multiple kinds of "triggers" raising the same event? 1 Answer
In delegate function, add the same listener event, why the same listener be called Immediately? 0 Answers
EventManager with parameters 1 Answer
Subscribe to an event when the application is loaded 3 Answers