Hey all!
I’m still quite new to C# and all its intricacies so sorry for this noob question.
I am trying to implement a mechanic for an endless runner where, upon button press, the player changes colour in order to phase through gates and obstacles.
However, in the early stages of getting this to work, I can’t seem to get my player to change colours.
Attached is my colour shift script and the event manager script I am using at the moment.
Colour Change Script:
using UnityEngine;
using System.Collections;
public class ColourChangeScript : MonoBehaviour {
private SpriteRenderer objectSprite;
private Color blue = new Color (0, 0, 1, 1);
private Color red = new Color (1, 0, 0, 1);
void Start ()
{
objectSprite = GetComponent <SpriteRenderer> ();
objectSprite.color = blue;
Debug.Log ("Colour has been set");
}
void OnEnable()
{
EventManager.OnClicked += ColourShift;
Debug.Log ("Subscribed");
}
void ColourShift ()
{
if (objectSprite.color == red)
{
objectSprite.color = blue;
Debug.Log ("Colour has changed!");
}
else if (objectSprite.color == blue) {
objectSprite.color = red;
Debug.Log ("Colour has changed!");
}
}
void OnDisable ()
{
EventManager.OnClicked -= ColourShift;
Debug.Log ("Unsubscribed");
}
}
EventManager Script:
using UnityEngine;
using System.Collections;
public class EventManager : MonoBehaviour {
public delegate void ClickAction ();
public static event ClickAction OnClicked;
void ButtonPress ()
{
if(Input.GetButtonDown ("Jump") == true)
{
Debug.Log ("Button did work");
if(OnClicked != null)
OnClicked();
}
}
}
Any help would be massively appreciated!