variable using in multiple objects at the same time

I’m trying to making a health variable system for objects, but I have a problem. When I assign the script with the variable to the instantiator script immediately after I instantiate the object(in this case a zombie), It goes away when I instantiate another zombie object, but works on the new one. Does somebody know how to get it so that when I instantiate the new object, the variable goes to the old AND the new one? Please show some code that maybe make it work;)

Kind regards
Tom

Are you using addComponent to add a new health script when you instantiate a zombie?
In reality though, you’re probably better off creating a zombie prefab that has the health script already attached to it so when you instantiate the zombie, it already has the script on it.

I already have the prefab, sorry for not mentioning

can we see your code, something doesn’t sound right in the description of it…

    public GameObject zombie;
    GameObject clone;

    public Entity player;

    int attackCount;

    Vector3 pos;

    private Zombie zombieScript;

    // Use this for initialization
    void Start ()
    {

        clone = Instantiate (zombie, new Vector3(35,35), Quaternion.identity) as GameObject;

        attackCount = 0;

        zombieScript = clone.GetComponent<Zombie> ();

    }
   
    // Update is called once per frame
    void Update ()
    {
       
        if (Input.GetKeyDown (KeyCode.A)) {

            attackCount++;

            if (attackCount >= 10) {

                pos = new Vector3 (player.transform.position.x, player.transform.position.y);

                clone = Instantiate (zombie, pos, Quaternion.identity) as GameObject;

                zombieScript = clone.GetComponent<Zombie> ();

                attackCount = 0;

            }
               
            zombieScript.health -= 1f;

            Die ();

        }


    }

    void Die () {

        if (zombieScript.health <= 0) {

            Destroy (clone, 0);

        }
   
    }

I currently can’t look to closely at your script. Maybe @LeftyRighty can, but just at a glance it looks like you’re spawning new zombies and only grabbing their script. Which means you’re only targetting the new zombies script. However you plan to do damage to the zombie, you need to target that zombies script. If it’s based off of shooting a zombie, then when a bullet hits the zombie, you check that zombies health.

If it’s a over time health decline, then each zombie has to have a health reduction in an update or one master update that loops through all zombies and updates their health.

It might help to describe the behavior you’re trying to achieve with this code. I definitely see problems but I don’t know how to suggest a fix because I’m not sure what it’s supposed to be doing or how your game plays.

1 Like

I’ll join the “what is this supposed to do” club… what is that script attached to, because it looks like you’re trying to remotely manipulate the zombies from the player… or something a little odd.

One of the basic “tenets” is that each object should handle it’s own properties and functionality and expose functions to other objects to tell it to do things.

Without knowing quite what you’re going for, I would imagine a “zombie game” would have:
the instantiation of the zombies done by some sort of level/game manager which is keeping a track of them,
the zombies have their own locomotion script, AI script, health script which makes them do things on their own independently,
the player object has scripts to handle the input, players health, locomotion, weapons etc.

Keep it all nice and modular.

1 Like