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);
}
}
}