Referencing objectHealth from a different script?

Hey all,
I have a simple scene in which I have a Cell and a Photon game object. I also have two scripts ‘BoardManager’ and ‘CellControl’. At the moment CellControl has nothing in it.

At the momennt I can move the Photon around and the int ‘cellHealth’ will descrease every time the Photon moves. Later on I will have lots of Photons all moving downwards at a set rate so I tied the CellHealth to the Photon movement. I have control over the Photon movement for testing purposes at the moment.

Collisions work fine. When a Photon collides with a Cell it adds 1 health to cellHealth in the inspector. Unfortunately i can’t figure out how to tie this to the cell gameobject rather than the photon. I want to Cell to die if cellHealth reaches zero, but at the moment everything is tied to the photon. I know why this is (the BoardManager script is attached to the Photon). But I don’t know how to attribute cellHealthh to the Cell. I have tried lots of things but I just end up with endless errors.

I’m thinking I could use gameObject.getCOmponent in the CellControl script but I’m having issues getting my head around it or figuring out how I would implement it. Screenshot here if it helps anyone visualise my scene. Photon is the small white dot, Cell is in green. The dark grey square is a wall (which works fine for me but isn’t related to this issue).

BoardManager

using UnityEngine;
using System.Collections;

public class BoardManager : MonoBehaviour {

//INTEGER OF CELL'S CURRENT HEALTH TOTAL
public int cellHealth = 10;

public GameObject Photon;
// For movement
Vector3 pos;  
// Speed of movement
float speed = 5f;                       

//Triggers +cellHealth on collision with Cell
void OnTriggerEnter2D(Collider2D col) {
    if(col.gameObject.name == "Cell") {
        //GIVE THE CELL +1 HEALTH HERE
        cellHealth++;
        Debug.Log ("+health collision");
    }
    gameObject.SetActive(false);
}

// Use this for initialization
void Start () {
    // Take the initial position
    pos = transform.position;
}

// Update is called once per frame
void FixedUpdate () {
    if(Input.GetKey("down") && transform.position == pos) {        
        pos += Vector3.down;
        cellHealth --;
    }
    if(Input.GetKey("up") && transform.position == pos) {        
        pos += Vector3.up;
        cellHealth --;
    }
    if(Input.GetKey("left") && transform.position == pos) {        
        pos += Vector3.left;
        cellHealth --;
    }
    if(Input.GetKey("right") && transform.position == pos) {        
        pos += Vector3.right;
        cellHealth --;
    }
    //Change this for instant movement
    transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);    // Move there

    if (cellHealth <= 0) {
        Debug.Log ("PHOTON died when it should have been CELL");
        gameObject.SetActive(false);
    }
}
}

You need a script attached to the Cell with the Cell’s health as public variable. Then you can use GetComponent to change that variable from the other script. Alternatively (and that is probably the way I would do it, if there is more than one Cell), you can test for collision with the Photon in the Cell’s script and change the Cell’s health directly. If you have only a single Cell, I would probably use the first variant, but instead of GetComponent in OnTriggerEnter2D, I would add a variable to the BoardManager and drag the Cell-Script there via the Inspector (or set it via code if the cell is instantiated during runtime).

Version A:

Cell.cs:

public class Cell : MonoBehaviour {
    public int health;
}

BoardManager.cs:

//...
void OnTriggerEnter2D(Collider2D col) {
     if(col.gameObject.name == "Cell") {
         col.gameObject.GetComponent<Cell>().health++;
     }
     gameObject.SetActive(false);
}
//....

Version B:

Cell.cs:

public class Cell : MonoBehaviour {
     public int health;
    //....
    void OnTriggerEnter2D(Collider2D col) {
         if(col.gameObject.name == "Photon") {
             health++;
         }
    }
    //...
}

You are on the right track.

I assume you have the CellControl script on the cell game object? If not, do that.

Move cellHealth to the CellControl script.

In your OnTriggerEnter2D, where you have the comment “//GIVE THE CELL +1 HEALTH HERE” you can get the CellControl script from the object you just hit like this:

CellControl cell = col.gameObject.GetComponent<CellControl>();

Make sure you check that cell isn’t null (if it is, the thing you hit doesn’t have a CellControl script on it).

Now you can access the cell health:

cell.cellHealth

Although you probably want to rename it to just Health.

Hey, I’m just going to take a shot at this, hopefully steers you in the right direction.
So I would separate these into 3 different scripts. I would attach the BoardManager to an empty gameObject in your scene, positioned at 0,0,0. On this BM script I would leave the single photon movement for now.

On your Cell script I would declare the health and handle the collision, checking against a gameobject tagged “Photon”. When health reaches 0, either destroy or deactivate.

The single Photon for now doesn’t have to do anything while you are manually moving it from BM, but in future they will need some form of automation.

Good luck, Hope this helps :slight_smile: