I cant find the problem in the code

Posting just an error message like this is not entirely helpful.
All we know is that your cursors dont work.

We cant see your code or your inspector so you have essentially made it impossible for anyone to help you without asking lots of questions and frustrating all parties.

One of the key points is providing as much USEFUL information as possible without detracting from the issue.

The code obviously is stating a bug at the line 34 so compare the code to the one on screen there.

I suggest if you havent taken our 3D course this might benefit you as you will learn to bug hunt properly and also ask questions so that people can have the information to hand to help you.

Hi Muhammad, welcome to the community :slight_smile:

The code, for the lecture you are currently at, for CursorAffordance.cs is as follows;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CursorAffordance : MonoBehaviour 
{
    [SerializeField] Texture2D walkCursor = null;
    [SerializeField] Texture2D unknownCursor = null;
    [SerializeField] Texture2D targetCursor = null;
    [SerializeField] Vector2 cursorHotspot = new Vector2(96, 96);
    CameraRaycaster cameraRaycaster;
 	
    // Use this for initialization
    void Start () 
    {
        cameraRaycaster = GetComponent<CameraRaycaster>();
    }

    // Update is called once per frame
    void Update() 
    {
        switch (cameraRaycaster.layerHit)
        {
            case Layer.Walkable:
                Cursor.SetCursor(walkCursor, cursorHotspot, CursorMode.Auto);
                break;
            case Layer.RaycastEndStop:
                Cursor.SetCursor(unknownCursor, cursorHotspot, CursorMode.Auto);
                break;
            case Layer.Enemy:
                Cursor.SetCursor(targetCursor, cursorHotspot, CursorMode.Auto);
                break;
            default:
                Debug.LogError("Don't know what cursor to show");
                return;
        }
    }
}

The error message you are seeing is the one that you have written in within the switch statement, under the default case, e.g. if none of the other cases are true, then use this one.

So, assuming your code is correct, we can reasonably deduce that the layer which is reported in cameraRaycaster.layHit has been unrecognised, e.g. it wasn’t Walkable, RaycastEndStop_ or Enemy.

My approach to resolving your error would be to first check that your code is correct, not just the above script, but also the CameraRaycaster, as this is used within the switch statement.

Then I’d probably want to check what result was coming through for layerHit being used in that switch statement, we could use a Debug.Log statement here to output the value to the console, for example;

private void Update()
{
    Debug.Log("layerHit = " + cameraRaycaster.layerHit);

    switch(cameraRaycaster.layerHit)
    {
        // ... existing code
    }
}

Depending on what was output to the console may well determine my next course of action, but I’d probably be looking to see if I could reproduce the error in the exact same way each time. We know that the layer is unrecognised, so for example, what are you placing the mouse over when this error occurs? Does it only occur on this object or does it occur on other objects too?

Give the above a go and let’s see if we can track the issue down and get you moving forward again :slight_smile:

1 Like

Read my post back and Ouch that was a little harsh. Apologies and thanks Rob

Something that might help here is a piece of software i have been using to speed up my response time on the Q&A is code compare. You literally can copy and paste code in the two panels to compare them :slight_smile:
You will need to understand the code a bit as obviously it will detect differences that are meant to be there as well but it should help find typos or mistakes easier for null referencing and the like.

Hopefully this will be a bit more helpful than my half awake first post!

1 Like

thanks for all the help.Apparently my problem was fixed.I remove component of player movement in heirarchy and drag it back.It works thanks again for all the help

2 Likes

Hi Muhammad,

Glad you were able to resolve your issue and can move forward again :slight_smile:

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

Privacy & Terms