- Home /
Subtract value from one script to another
Hello community
I've been having some difficulties with trying to communicate with my health int from character to subtract from the value of setEnemyDamage int. What I'm trying to pull off is setting the damage from an Obstacle quickly in public so I can change the value per each scene level for more of a challenge as the player progresses. My playerHealth seems to pick up health int outcome, but when I go over top the Obstacle trigger collider it appears that it has it's own int and doesn't subtract from character health script. I'm using 5.3 version as this is a school project. If any answers to this question soon would be greatly appreciated!
Obstacle script
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Obstacle : MonoBehaviour {
public GameObject playerObject;
public int setEnemyDamage;
private Character ScriptAccess;
private int playerHealth;
// Use this for initialization
void Start () {
ScriptAccess = playerObject.GetComponent<Character>();
playerHealth = ScriptAccess.health;
Debug.Log(playerHealth);
}
// Update is called once per frame
void Update () {
}
//Avoids destroying player on collision, also destroys projectiles
void OnTriggerEnter2D(Collider2D c)
{
if (c.gameObject.tag == "Projectile") {
Destroy (c.gameObject);
}
if (c.gameObject.tag == "Player")
{
playerHealth -= setEnemyDamage;
Debug.Log(playerHealth);
}
}
}
Character script
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Character : MonoBehaviour {
public Rigidbody2D rb; // Public. Accessible through Inspector. Must drag object/component into Script.
public float jumpForce; // Pubilc. Let's user update value through Inspector.
// Handles movement and jumping
public int health; // Player Health
public bool isGrounded; // Used to see if character is on the floor, platform, etc...
public LayerMask isGroundLayer; // Used to list what is Ground
public Transform groundCheck; // Used to check ground collision
public int speed; // Used to give character a speed.
public int coinCollection; // Player coins collection
public int powerCoins; // Player power up coins
// Handles animations
public Animator anim; // Used to access the animator controller
int state; // Used to change state of character
// Handles projectiles
public Transform projectileSpawnPoint; // Whatever the teacher did here
public Rigidbody2D projectilePrefab; // I was forced to write this
// Controls facing direction
public bool facingRight;
// Use this for initialization
void Start () {
// Link component to variable to be used later.
rb = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
// Check that GetComponent worked.
// Works if there is a Rigidbody2D on the object the Script is linked to.
if (!rb) // Is there no Rigidbody2D.
Debug.Log ("No Rigidbody2D attached.");
if (!anim) // Is there no Animator.
Debug.Log ("No Animator attached.");
}
// Update is called once per frame
void Update () {
if (health <= 0)
{
health = 0;
Destroy(gameObject);
}
// Check if character is on ground
isGrounded = Physics2D.OverlapCircle (groundCheck.position, 0.2f, isGroundLayer);
// Check if Space is pressed.
if (Input.GetButtonDown ("Jump") && isGrounded) {
// Player wants to Jump.
// Make character jump.
Debug.Log("Jump");
anim.SetTrigger ("Jump");
// Use Rigidbody to move character
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
// Check if Fire is pressed.
if (Input.GetKeyDown (KeyCode.LeftControl)) {
// Player wants to Fire.
// Make character fire.
fireProjectile();
}
// Make character move left or right, if grounded
// GetAxisRaw returns -1,0,1
// GetAxis returns -1->1
float moveValue = Input.GetAxisRaw ("Horizontal");
// Make character move left or right.
rb.velocity = new Vector2 (moveValue * speed, rb.velocity.y);
// Change state parameter
anim.SetFloat ("Move", Mathf.Abs (moveValue));
// Check if character should look Left or Right
if ( (moveValue > 0 && !facingRight) ||
(moveValue < 0 && facingRight))
flip ();
// Check if player is on ground, if so Jump
anim.SetBool ("IsGrounded", isGrounded);
} // Closes Update()
// Function to flip Character in direction it's moving
void flip()
{
// Toggle facingRight variable
facingRight = !facingRight;
// Make a copy of old scale
Vector3 scaleFactor = transform.localScale; // Scale is (1,1,1);
// Flip the scale
scaleFactor.x *= -1; // Changed to (-1,1,1)
// Update the scale to flipped value
transform.localScale = scaleFactor;
}
void fireProjectile()
{
// Make character fire.
Debug.Log ("Pew Pew");
// Change state parameter
anim.SetTrigger ("Attack");
if (facingRight) {
Instantiate (projectilePrefab, projectileSpawnPoint.position,
Quaternion.Euler (new Vector3 (0, 0, 0)));
} else if (!facingRight) {
Instantiate (projectilePrefab, projectileSpawnPoint.position,
Quaternion.Euler (new Vector3 (0, 180.0f, 0)));
}
}
// Collectiable items, and stored into player database.
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Collectible")
{
coinCollection++;
Destroy(col.gameObject);
Debug.Log("A COIN!");
}
if (col.gameObject.tag == "PowerCoin")
{
powerCoins++;
Destroy(col.gameObject);
Debug.Log("POWER UP!");
}
}
}
Thank you for taking the time to read.
Answer by Gothic_Anatomist · Oct 16, 2016 at 09:09 AM
Instead of referencing the variable in your character script directly from your obstacle, you could create a method in the charaacter script to change the value - such as:
public void DecreaseHealth (int damage)
{
health -= damage;
Debug.Log(health);
}
The you call it from your obstacle script and pass in the setEnemyDamage value:
if (c.gameObject.tag == "Player")
{
scriptAccess.DecreaseHealth (setEnemyDamage);
}
I hope that helps :)