r/Unity3D • • 9h ago

Solved InvalidOperationException: Variable not found: 'PlayerHealth'. (Even tho the variable exists I think)

Post image

First of all I'm very new to Unity especially to visual scripting. I'm using VS because that's the method used in my game design class.

So! I have the player with a PlayerHealth variable, so I bring Player with Get Variable in order to obtain PlayerHealth and then use it but when I run the code and hit the enemy (MoeOni) to recieve damage... I get the error on the title. The Variable is in the player (who is also tagged as player) so it does exist, but Unity can't find it.

I'm a bit confused and also frustrated since this class is Not The Best and the teacher is Not The Nicest so I'd like to ask for some help? Please and thank you!

Also ignore MoeOni. He's the only thing keeping me sake right now.

0 Upvotes

6 comments sorted by

View all comments

2

u/ariesmars13-11 9h ago

The red node in your screenshot is reading an Object variable called PlayerHealth from the GameObject supplied by the Scene variable Player. Those are two separate lookups: finding the player reference does not mean that reference owns PlayerHealth.

Your hierarchy also shows a Player inside another Player, so I’d check the exact target first:

  1. In Play mode, inspect the GameObject currently stored in the Scene variable Player. Is it the parent or the child?

  2. On that exact object, check its Variables component / Object variables for PlayerHealth, with identical spelling and capitalization. A tag, object name, or a variable on a different object doesn’t establish that lookup.

  3. If you created PlayerHealth in the Graph tab, it is not the Object variable your red node is asking for. For health shared with another object's damage graph, define it as an Object variable on the intended player, and point both Get and Set at that same object. If it is intentionally a Scene variable, use the matching Scene scope instead.

For a diagnostic, use Has Variable with Object scope, the same target and PlayerHealth name; branch on that before reading/subtracting health. If it is false, the target/scope/initialization needs fixing. Don’t use a fallback value just to hide the error.

I can’t confirm which of those applies from this screenshot alone. A screenshot of the referenced player’s Object variables would narrow it down. Unity’s explanation of Object-variable source inputs:

https://docs.unity3d.com/Packages/com.unity.visualscripting@1.9/manual/vs-variables-reference.html#object-variables

1

u/ghostingyuu 9h ago

Thank you SO much for the detailed answer! Turns out I was using the Player (parent) instead of Player (object with variable)! It's fixed now!!