Help with a Health System

Hey Everyone!

So I’ve tried to follow along with this tutorial to create a similar health system

Everything appears to be working up until the 8:30 mark where he drags his HealthController GameObject into the script. Every time I try to drag and drop my game object it doesn’t allow me.

It’s worth noting that my game is 3d and I did change his collider code from 2D to 3D (as he mentioned in his video)

Anyone have any thoughts on why this isn’t working? I’m sure it’s some simple thing but this is a new system to me.

For reference, the prefab I am trying to apply the script to is simply a sphere with a Sphere Collider and a Rigidbody

if you need any more information to support this thread please comment. Thanks in advance

EDIT:
If I drag my prefab into my scene hierarchy it adds the HealthController fine. But when i drag the prefab back to my assets folder it removes it and I cannot add it. I am reading now that prefabs cannot reference gameObjects.

Can anyone suggest a work around? Essentially I have a gameobject (empty object with health controller script attached), and a bullet I want to instantiate from an enemy

More generally nothing in an asset (something on disk) can reference something in the scene.

Make the bullet prefab, do NOT place it in the scene

Make the enemy prefab, complete with his shooting script. Do not place it in scene.

In that enemy’s shooting script prefab, reference the bullet prefab.

Now in your enemy spawner, give it the prefab for the enemy.

If you want to troubleshoot your current setup more, here’s how:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

thanks for the reply.
I should have been more clear, I have the shooting system working fine along with the collisions / destroy projectiles . I also have a functioning Health system through canvas / images and a health controller script.

I’m just stuck at this final step in where I I cannot assign my health controller game object to the “player damage” script attached to my bullet prefab