My delegates are running fine, BUT I want to revery cursor after 2 seconds

rpg_s01_using_delegates_in_c

#1

I’m trying to build on our work so far and have hte cursor flip back to the defaul arrow after 2 seconds if there has been no layer change, but it’s not working! Anyone hacve a similar problem or solution ideas?

Here is my CursorAffordance script:

    using System.Collections;
    using UnityEngine;

    public class CursorAffordance : MonoBehaviour {

	[SerializeField] Texture2D[] cursorSprites;  // =: [WalkArrow, TargetSword, UnknownQuestionMark]
	[HideInInspector] public CameraRaycaster raycaster;

	Vector2 _hotspot = Vector2.zero;
	Texture2D _texture;

	[SerializeField] float resetTime = 2f;

	IEnumerator _coroutine;


	void Start () {
		_texture  = Texture2D.blackTexture;  //ugly initialiser
		raycaster = GameObject.FindWithTag("MainCameraRig").GetComponent<CameraRaycaster>();
		Cursor.SetCursor(cursorSprites[0], _hotspot, CursorMode.Auto);
		CameraRaycaster.OnLayerChange += GetTexture;
		_coroutine = ResetSoon();
	}
	


	void GetTexture(Layer layer){   // method called OnLayerChange
		StopCoroutine(_coroutine);
		switch (layer)
		{
			case (Layer.Walkable):
				_texture = cursorSprites[0];
				break;

			case (Layer.Enemy):
				_texture = cursorSprites[1];
				break;
			
			case (Layer.RaycastEndStop):
				_texture = cursorSprites[2];
				break;

			case (Layer.Default):
				_texture = cursorSprites[2];
				break;
			
			default:
				_texture = cursorSprites[0];
				break;
		}
		
		Cursor.SetCursor(_texture, _hotspot, CursorMode.Auto);
		StartCoroutine(_coroutine);
	}

	//TODO make this work right, maybe using Events and Delegates
	IEnumerator ResetSoon()
	{
		yield return new WaitForSeconds(resetTime);
		print("revery cusror");
		Cursor.SetCursor(cursorSprites[0], _hotspot, CursorMode.Auto);
		yield return null;
	}
	
    }

and my CameraRaycaster script:

public class CameraRaycaster : MonoBehaviour
{
    public Layer[] layerPriorities = {
        Layer.NonPlayerChar,
        Layer.Enemy,
        Layer.Animal,
        Layer.Player,
        Layer.Walkable,
        Layer.Default,   
        Layer.RaycastEndStop,
    };

    public delegate void D_OnLayerChange(Layer l);
    public static event D_OnLayerChange OnLayerChange;  //event's accessor (public) must be <= delegate's


    float distanceToBackground = 1000f;
    public Camera viewCamera;

    RaycastHit _hit;

    public RaycastHit hit
    {
        get { return _hit; }
    }



    Layer _layerHit;

    public Layer layerHit
    {
        get { return _layerHit; }
    }



    void Start()    
    {
        _hit.distance = distanceToBackground;
        _layerHit = Layer.RaycastEndStop;
    }


    void Update()
    {
        if (!Input.GetMouseButtonDown(0)) return; //Only call on click

        foreach (Layer layer in layerPriorities){

            RaycastHit? __hit = RaycastForLayer(layer);

            if (__hit != null){
                _hit = (RaycastHit)__hit;
                if (_layerHit != layer){
                    _layerHit = layer;
                    OnLayerChange(layer);
                }
                return;
            }
        }

        // Otherwise return background hit
        _hit.distance = distanceToBackground;
        _layerHit = Layer.RaycastEndStop;
        OnLayerChange(Layer.RaycastEndStop);
    }

    RaycastHit? RaycastForLayer(Layer layer)
    {
        int layerMask = 1 << (int)layer; 
        Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
        
      
        RaycastHit hit1; // used as an out parameter
        bool hasHit = Physics.Raycast(ray, out hit1, distanceToBackground, layerMask);
        if (!hasHit) return null;
        return hit1;
       
    }

}

Any help would be loved! I’ll make it up by reading through a few posts below and seeing if I can help there (which is what I always try to do when I ask for help. Help two people, get help from one, is a good ratio!).