How could I give an object a variable without a script?

I know you’re thinking either I’m stupid because it’s not possible or I don’t know the answer, but I was wondering if there was a way to give my object that is instantiated a float but not assign it a script. The reason is because this float is the health for an instantiated prefab (which in my case is a zombie). The health is modified in my bullet script, so the only thing in my zombie script is just “public float health;” and I assign it In the inspector. This all works perfectly fine. The only thing is it says it’s assigned but not used (obviously), and I’m okay with that but I want to know if theres a way to make it so it doesn’t say that without adding useless code within that script to remove that. Thanks

To address your first question, thinking outside the box, you could name your object with a value (i.e. = health.ToString()) where health is a float. Then you could grab the name and convert it back to a float in another script.

I have to say though, I really don’t recommend doing that. I’m trying to understand what you really want to do here. Is your only concern that the compiler issues a warning?

Typically, it’s better to have getters and setters or some public API for your class to access data. For instance, the way your class is now, any other code anywhere can come along and modify Health because it is public. Well you probably don’t care since you’re writing all the code, and that’s probably a valid thing.

But say your game hits it big and you have to hire 5 more programmers. One of them takes a shortcut with a bomb and accidentally increases the health instead of decreases it. Now you have to find a bug. I’m setting this all up to describe the quick fix, sorry.

Basically, just add a function similar to “Hit()” to your zombie class, and the Hit function then determines what happens, how much health is removed, if enough health was removed to kill the zombie, etc. That Hit method could return true if you killed the zombie (health = 0), or it could even just return current health. If killed, it could start a death scene animation located on its own object, instead of the bullet having to determine if the zombie death scene starts.

You could also then introduce passing in an enumerated type for WHAT is hitting the zombie (axe, machete, bullet, etc). Anyway, instead of the bullet class modifying Health directly, it just calls Hit() in this example. That will make the warning go away, and it’s better for future modifications.

If you still need an “initial health”, then create a new public float initialHealth variable, set that in the prefab, and set the private health variable to initialHealth in the Start() method.

Use c# for your zombie health script and add this pragma to the top:

#pragma warning disable 0168

This line disables the warning message for a variable that is declared but never used.

Other useful pragmas

#pragma warning disable 0219 // disable message: variable is assigned but never used
#pragma warning disable 0414 // disable message: private field is assigned but never used

You could move what would have been the zombie script into another class. Say this is the thing that spawns zombies:

// inside zombieSpawner.cs:

class zombie_t {
  public Transform T;
  public int hitPoints;
  // everyone loves constructors:
  public zombie_t() {}
  public zombie_t(Transform zt, int hits) {
    T=zt; hitPoints=hits;

LinkList<zombie_t> Zombies;

public makeNewZombie() {
  Transform T = Instantiate(zombiePrefab...);
  zombie_t Z = new zombie_t(T, 100);

So now zombie_t is the script that would have been on the zombies. But is now stored, and held in a list, over here.

The problem is, you will probably want to give the zombies OnCollisionEnter and so on. There could have been a way to write that in one script, and attach it to the zombies, but there isn’t – you need a script on the zombies for that. Also for raycasts, it’s a pain to search Zombies for the matching transform. Much easier for the zombies to have a small script knowing their own number, or linking back to their zombie_t.

In the end, in Unity, it’s just easier to put all the zombie-handling code on the zombie.