Hi, as the title suggests I am having troubles with accessing other variables in other scripts on other GameObjects. I have a player script for the health of my player(playerShield) and another script for enemy damage(EnemyScript). What I would like to happen is when the player hits the enemy collider the player takes damage. But the reverse happens, the player doesn’t take any damage and it comes up with this error: NullReferenceException: Object reference not set to an instance of an object
EnemyScript.Damage () (at Assets/Scripts/AI/EnemyScript.cs:73).
When I click on the error it directs me to line 73 on the EnemyScript.
Here is my EnemyScript:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyScript : MonoBehaviour
{
public float enemyShield;
public DestroyRange destroyRadius;
public PlayerShield shieldValue;
public GameObject player;
public Text EnemyHealth;
public GameObject enemy;
public bool random = true;
public bool damage = false;
public bool textTrigger = true;
public float damageAmount;
void Awake ()
{
if(random == true)
{
enemyShield = Random.Range( 0.1f, 6.0f );
}
shieldValue = GetComponent<PlayerShield>();
destroyRadius = GetComponent<DestroyRange>();
Debug.Log("Enemy Spawned!!!");
}
// Update is called once per frame
void Update ()
{
if(textTrigger == true)
{
EnemyHealth.text = enemyShield.ToString();
}
enemyShield = Mathf.Round(enemyShield * 10f) / 10f;
}
private GameObject GetGameObject()
{
return gameObject;
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Enemy Hit!!!");
if(other.CompareTag("Player"))
{
Debug.Log("Enemy Hit Player!!!");
if(damage == true)
{
Debug.Log("Damage = true");
Invoke("Damage", 0.1f);
}
if(other.gameObject.GetComponent<PlayerShield>().shieldValue > enemyShield)
{
Destroy(enemy);
Debug.Log("Enemy Destroyed!!!");
}
}
}
void Damage()
{
Debug.Log("Damage invoked!!");
transform.Find("Player").GetComponent<PlayerShield>().shieldValue = transform.Find("Player").GetComponent<PlayerShield>().shieldValue - damageAmount;
Invoke("DamageTrue", 3.0f);
Debug.LogError("ERROR: Damage Invoke didn't Cancel.");
}
void DamageTrue()
{
damage = false;
CancelInvoke("Damage");
CancelInvoke("DamageTrue");
}
}
And here is the PlayerShield script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Image=UnityEngine.UI.Image;
public class PlayerShield : MonoBehaviour
{
public float maxShields = 5.0f;
public float shieldValue = 1.0f;
public float powerUpValue = 0.1f;
public float enemyPowerUpValue;
public float pHealth;
public bool objective;
public Image Healthbar;
public Text shieldNumber;
public EnemyScript enemyShield;
public EnemyScript damage;
public void start()
{
objective = false;
shieldValue = 1.0f;
enemyShield = GetComponent<EnemyScript>();
damage = GetComponent<EnemyScript>();
}
void Update()
{
if(shieldValue == 5.0f)
{
objective = true;
Debug.Log("shieldValue = 5.0f");
}
pHealth = shieldValue / maxShields;
Healthbar.fillAmount = pHealth;
shieldNumber.text = shieldValue.ToString();
if(shieldValue > maxShields)
{
shieldValue = maxShields;
}
if(shieldValue <= 0)
{
Invoke("Respawn", 0.1f);
}
shieldValue = Mathf.Round(shieldValue * 10f) / 10f;
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("PowerUp"))
{
shieldValue = shieldValue + powerUpValue;
Debug.Log("you got a power UP!");
}
if(other.CompareTag("Enemy"))
{
if(other.gameObject.GetComponent<EnemyScript>().enemyShield > shieldValue)
{
if(other.gameObject.GetComponent<EnemyScript>().damage == false)
{
Invoke("Respawn", 0.1f);
Debug.Log("Player Destroyed!!!");
}
}
if(other.gameObject.GetComponent<EnemyScript>().enemyShield < shieldValue)
{
shieldValue = shieldValue + enemyPowerUpValue;
}
}
}
void Respawn()
{
Application.LoadLevel(Application.loadedLevel);
}
}
Any form of help is greatly appreciated and thankyou in advance.