Hello, I am having some difficulty in how to send a message over to another component in a different game object without having errors such as the NullReferenceException.
From what I understand from that error is that something is not given a value and is returning null because of it but I don’t see anywhere in the codes that may hints that. However the compiler says that the error occurs in the line that states : “alternateAttackManager.SendMessage (“GatherEnergy”, 20.0f);”
Here are two codes that I am working with to make this happen.
The first one.
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 10.0f;
private float currentHealth;
public GameObject deathEffect;
public GameObject damageEffect;
public string collisionTag = "PlayerBullets";
public int theEnergy = 20;
private GameObject alternateAttackManager;
// Use this for initialization
void Start ()
{
currentHealth = maxHealth;
alternateAttackManager = GameObject.FindGameObjectWithTag ("AlternateAttackManager");
}
void OnTriggerEnter(Collider col)
{
if(col.tag == collisionTag)
{
currentHealth -= 1.0f;
Instantiate(damageEffect,col.ClosestPointOnBounds(transform.position), Quaternion.identity);
if(currentHealth <= 0)
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
//THIS LINE OF CODE IS THE POINT OF INTEREST
alternateAttackManager.SendMessage ("GatherEnergy", 20.0f);
}
}
}
}
The second one.
using UnityEngine;
using System.Collections;
public class ChangeToSecondForm : MonoBehaviour
{
private static ChangeToSecondForm instance = null;
public static ChangeToSecondForm Instance
{
get {return instance; }
}
private GameObject playerToBeChangedFrom;
private GameObject playerChangingTo;
public Transform secondFormDisplay;
public float maxEnergy = 100.0f;
public float currentEnergy;
private float energyOriginalXScale;
// Use this for initialization
void Start ()
{
currentEnergy = 0.0f;
energyOriginalXScale = secondFormDisplay.localScale.x;
playerToBeChangedFrom = GameObject.FindGameObjectWithTag ("PlayerCharacter");
playerChangingTo = GameObject.FindGameObjectWithTag ("PlayerCharacterSecondForm");
playerChangingTo.SetActive (false);
}
// Update is called once per frame
void Update ()
{
if (currentEnergy <= 0)
{
secondFormDisplay.GetComponentInChildren<Renderer>().enabled = false;
}
SecondForm ();
}
public void SecondForm()
{
if (Input.GetButtonDown ("TransformToSecond") && (currentEnergy == 100))
{
Debug.Log("Q has been pressed");
currentEnergy = 0;
playerToBeChangedFrom.SetActive (false);
playerChangingTo.SetActive(true);
playerChangingTo.SendMessage ("TransformQueSound", true);
Invoke ("ChangeBack", 10.0f);
}
}
public void ChangeBack ()
{
playerToBeChangedFrom.SetActive (true);
playerChangingTo.SetActive (false);
}
//////////THIS BODY OF CODE IS WHERE THE SEND MESSAGE PUTS THE VALUE INTO
public void GatherEnergy(float energyGathered)
{
Debug.LogError ("Got Energy from dead enemy.");
currentEnergy += energyGathered;
if (currentEnergy >= 100)
{
currentEnergy = 100.0f;
}
secondFormDisplay.localScale = new Vector3(energyOriginalXScale * (currentEnergy/maxEnergy), secondFormDisplay.localScale.z, secondFormDisplay.localScale.z);
}
}
If someone can spot the issue here I would really appreciate it because I am drawing a blank at the moment. Will still try to debug this myself so any help I can get will be awesome.