Be the first to post for '23_MA_RPG'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

@ben I am getting a weird error when I position my target (enemy) cursor over my enemies. For some enemies it works; for others it doesn’t. Anyone else having the same problem?

pls post your Error.
I got a null reference error. Fixed it with

        RaycastHit hitInfo;
        Physics.Raycast(ray, out hitInfo, maxRaycastDepth);
        if (hitInfo.transform != null)  <---
        {
            var gameObjectHit .....
            var enemyHit ......

            if (enemyHit)
            {
                Cursor.....
            }
        }
2 Likes

I was getting the null reference error as well. I had already solved it using the fix that you suggested but I am still getting a few enemies that won’t allow for the cursor change.

In order to call GetTransformInfoExpectUpToDate, RendererUpdateManager.UpdateAll must be called first.
UnityEngine.Camera:Render()
UnityStandardAssets.Water.Water:OnWillRenderObject() (at Assets/Environment/Water/Water/Scripts/Water.cs:112)

I also get this error when trying to mouse over an enemy. I know that this is sometimes a problem with Unity 5.6.

[SOLVED] When I deleted the enemies that were closest to the water, it seemed to resolve my problem.

Glad you got this resolved guys, thanks for helping each other.

Hello All

I am experiencing this Null Exception Error:

NullReferenceException: Object reference not set to an instance of an object
RPG.CameraUI.CameraRaycaster.RaycastForEnemy (Ray ray) (at Assets/_CameraUI/CameraRaycaster.cs:66)
RPG.CameraUI.CameraRaycaster.PerformRaycasts () (at Assets/_CameraUI/CameraRaycaster.cs:58)
RPG.CameraUI.CameraRaycaster.Update () (at Assets/_CameraUI/CameraRaycaster.cs:50)

@69Unbalance…I tried replacing my script with your suggestions but I get an error that says
"The name `enemyHit’ does not exist in the current context".

This is my code from the video…

   bool RaycastForEnemy(Ray ray)
        {
    RaycastHit hitInfo;
    Physics.Raycast(ray, out hitInfo, maxRaycastDepth);
    var gameObjectHit = hitInfo.collider.gameObject;
    var enemyHit = gameObjectHit.GetComponent<Enemy>();
    if (enemyHit)
    {
        Cursor.SetCursor(enemyCursor, cursorHotspot, CursorMode.Auto);
        onMouseOverEnemy(enemyHit);
        return true;
    }
        return false; 
}

And I tried to replace it with this…

  bool RaycastForEnemy(Ray ray)
        {
    RaycastHit hitInfo;
    Physics.Raycast(ray, out hitInfo, maxRaycastDepth);
    if (hitInfo.transform !=null)
    {
	var gameObjectHit = hitInfo.collider.gameObject;
    	var enemyHit = gameObjectHit.GetComponent<Enemy>();
    }        

    if (enemyHit)
    {
        Cursor.SetCursor(enemyCursor, cursorHotspot, CursorMode.Auto);
    }             
}

I would really appreciate any advice or further suggestions…
** Note: I just noticed that the error pops up at minute 19.41 for @ben but then it looks the video stops and resumes play and the errors are gone. Does this get address then in a later video?

You declared enemyhit in an if statement, you can only access it inside this if statement.

Just keep an eye on your brackets.

Thank you @69Unbalance, your advice on the brackets was spot on. I rewrote it and worked. But I am experiencing another issue.

Assets/_CameraUI/CameraRaycaster.cs(45,14): error CS0161: `RPG.CameraUI.CameraRaycaster.RaycastForEnemy(UnityEngine.Ray)’: not all code paths return a value

Below is the properly formatted code.

   bool RaycastForEnemy(Ray ray)
        {
            RaycastHit hitInfo;
            Physics.Raycast(ray, out hitInfo, maxRaycastDepth);
            if (hitInfo.transform != null)
            {
                var gameObjectHit = hitInfo.collider.gameObject;
                var enemyHit = gameObjectHit.GetComponent<Enemy>();


                if (enemyHit)
                {
                    Cursor.SetCursor(enemyCursor, cursorHotspot, CursorMode.Auto);
                    
                }
            }      
        }

Initially I thought it was related to the return true and return false statements. But when I add them into the code I dont get the errors in the console; at run time the cursors change from Target to Walk but my player now wont attack.

   bool RaycastForEnemy(Ray ray)
        {
            RaycastHit hitInfo;
            Physics.Raycast(ray, out hitInfo, maxRaycastDepth);
            if (hitInfo.transform != null)
            {
                var gameObjectHit = hitInfo.collider.gameObject;
                var enemyHit = gameObjectHit.GetComponent<Enemy>();


                if (enemyHit)
                {
                    Cursor.SetCursor(enemyCursor, cursorHotspot, CursorMode.Auto);
                    return true;
                }
                   return false;
            }      
        }

That’s simple, try to figure it out by yourself.

Assets/_CameraUI/CameraRaycaster.cs(45,14): error CS0161: `RPG.CameraUI.CameraRaycaster.RaycastForEnemy(UnityEngine.Ray)’: not all code paths return a value

This gives u the answer.

Sorry for blowing up this one message thread. Once again, brackets was the issue. My return false; was in the wrong place. This last code change finally removed all of the error logs in the console…My cursor changes but for whatever reason now my character cant attack, nor is the energy mechanic working. Ill start reviewing the video again.

bool RaycastForEnemy(Ray ray)
        {
            RaycastHit hitInfo;
            Physics.Raycast(ray, out hitInfo, maxRaycastDepth);
            if (hitInfo.transform != null)
            {
                var gameObjectHit = hitInfo.collider.gameObject;
                var enemyHit = gameObjectHit.GetComponent<Enemy>();


                if (enemyHit)
                {
                    Cursor.SetCursor(enemyCursor, cursorHotspot, CursorMode.Auto);
                    return true;
                }
                   
            }
            return false;
        }
1 Like

Today was a half day at work and I spent the other half trying (unsuccessfully) to debug my current issue. I honestly have no idea how to resolve this, changing the code back and forth does nothing lol. If I run the game with the code provided in the video I get the following Null Reference Error…

NullReferenceException: Object reference not set to an instance of an object
RPG.CameraUI.CameraRaycaster.RaycastForEnemy (Ray ray) (at Assets/_CameraUI/CameraRaycaster.cs:49)
RPG.CameraUI.CameraRaycaster.PerformRaycasts () (at Assets/_CameraUI/CameraRaycaster.cs:41)
RPG.CameraUI.CameraRaycaster.Update () (at Assets/_CameraUI/CameraRaycaster.cs:33)

But! even with this error, my player can still attack enemies and the energy mechanic works as per the end of the video.

When I add the code change provided by 69Unbalance above, The null exception error is resolved but my player loses the ability to attack enemies normally and the energy mechanic stops working. These 14 lines of code are the only ones that Im changing in the CameraRaycaster.cs.

If anyone would like to see additional screenshots of code snippets please let me know.

Post your full CameraRaycaster.cs.

Gladly…

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System;
using RPG.Characters; // So we can detectect by type

namespace RPG.CameraUI
{
    public class CameraRaycaster : MonoBehaviour
    {
        [SerializeField] Texture2D walkCursor = null;
        [SerializeField] Texture2D enemyCursor = null;
        [SerializeField] Vector2 cursorHotspot = new Vector2(0, 0);

        const int POTENTIALLY_WALKABLE_LAYER = 8;
        float maxRaycastDepth = 100f; // Hard coded value

        public delegate void OnMouseOverEnemy(Enemy enemy);
        public event OnMouseOverEnemy onMouseOverEnemy;

        public delegate void OnMouseOverTerrain(Vector3 destination);
        public event OnMouseOverTerrain onMouseOverPotentiallyWalkable;

        void Update()
        {
            // Check if pointer is over an interactable UI element
            if (EventSystem.current.IsPointerOverGameObject())
            {
                // Impliment UI interaction
            }
            else
            {
                PerformRaycasts();
            }
        }

        void PerformRaycasts()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            // Specify layer priorities below, order matters
            if (RaycastForEnemy(ray)) { return; }
            if (RaycastForPotentiallyWalkable(ray)) { return; }
        }

         bool RaycastForEnemy(Ray ray)
        {
            RaycastHit hitInfo;
            Physics.Raycast(ray, out hitInfo, maxRaycastDepth);
            if (hitInfo.transform != null)
            {
                var gameObjectHit = hitInfo.collider.gameObject;
                var enemyHit = gameObjectHit.GetComponent<Enemy>();


                if (enemyHit)
                {
                    Cursor.SetCursor(enemyCursor, cursorHotspot, CursorMode.Auto);
                    return true;
                }
                   return false;
            }      
        }

        private bool RaycastForPotentiallyWalkable(Ray ray)
        {
            RaycastHit hitInfo;
            LayerMask potentiallyWalkableLayer = 1 << POTENTIALLY_WALKABLE_LAYER;
            bool potentiallyWalkableHit = Physics.Raycast(ray, out hitInfo, maxRaycastDepth, potentiallyWalkableLayer);
            if (potentiallyWalkableHit)
            {
                Cursor.SetCursor(walkCursor, cursorHotspot, CursorMode.Auto);
                onMouseOverPotentiallyWalkable(hitInfo.point);
                return true;
            }
            return false;
        }
    }
}

I think I remember, Ben fix this bug wich comes from the audio trigger script,because of the collider

@69Unbalance I thank you very much for your reply. This is there any chance I could get you or @ben to elaborate a bit more? I am not sure that is my issue because I actually am not using the Audio Trigger. The script and prefab exist in the project folder only, but its not used anywhere in the scene.

I’ll give @69Unbalance a chance before I dive in.

Thank you Ben, I now know fir sure that my error was not caused by the “audio Trigger” but the explanation might still benefit other students. Ill post the solution to my original problem after work.

Just got to lecture 111 and now understand the discussion between the audio trigger and the camera raycaster. Thank you Ben.

"You may have issues clicking on enemies with the audio trigger because the audio trigger script spawns a collider that is being seen by raycast system and blocking it from actually get thru to the enemy. " - Lecture 111

Privacy & Terms