layerPriorities array does not return all the elements in method



My cursor isn’t changing because my layers failed to be detected when we need to find a layer match.
I have set layerPriority array in the inspector as 4 layers: 0 - default, 9 - walkable, 10 - enemy, 11 - storage. At start, I can see there are 4 layers, but when it comes down to the Find Top Priority Hit method, it can only detect 0, 9.
In Cursor Affordance I hae also set to 9, 10, 11

public class CameraRaycaster_v02 : MonoBehaviour
	[SerializeField] int[] layerPriorities;

// More code in between

	RaycastHit? FindTopPriorityHit (RaycastHit[] raycastHits)
		// Form list of layer numbers hit
		List<int> layersOfHitColliders = new List<int> ();
		foreach (RaycastHit hit in raycastHits)
			layersOfHitColliders.Add (hit.collider.gameObject.layer);

		// Step through layers in order of priority looking for a gameobject with that layer
		foreach (int layer in layerPriorities)
            Debug.Log("LAYERPRIORITIES " + layer);
            foreach (RaycastHit hit in raycastHits)
				if (hit.collider.gameObject.layer == layer)
					return hit; // stop looking
		return null; // because cannot use GameObject? nullable

What can I have done wrong? Would really appreciate any help!


Hm. So I tried entering the layer numbers in inspector in reverse order 11, 10. 9. 0 and now it works, the cursors are now behaving as expected.

Can someone please shed some light on what could possibly be happening here? Thanks.


Hi Samma,

Apologies for the long wait for a reply on this.
This could be down to the way the raycaster is working depending on if you have got as far as the bridges fix.
What the raycast does is that it fires out a ray and returns what it hits, But its not quite that simple it hits everything but at the moment returns any of the things it hits not necessarily in the correct order.

This meant if we tried to click on our bridge it would sometimes walk onto it and others try to walk under it on the terrain below depend on what the raycast returns.

I believe we fix this somewhere or in the event base raycasting lectures.

Let me know if this helps or if you found a solution already as it could be something within the code there too

automatically bumped #4


One of the other issues here may be that you have your default cursor set as a walk cursor so it will never show an unknown cursor there is likely other errors in the code like this that will be causing the issues.

closed #6

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.