My two sections of code are as follows:
using UnityEngine;
using System.Collections;
public class IsAsteroid : MonoBehaviour
{
public GameObject explosion;
public float health;
public GameObject FloatingDamagePrefab;
void Start ()
{
}
void Update ()
{
if (health <= 0)
{
Destroy(gameObject);
}
}
void OnTriggerEnter (Collider other)
{
if (explosion != null)
{
Instantiate(explosion,transform.position,transform.rotation); //Create a new explosion instance when hit
}
if (other.tag == "Bullet")
{
health = health - 10;
Vector3 textLocation = Camera.main.WorldToViewportPoint(transform.position);
textLocation.z += 0.005f;
textLocation.x += 0.005f;
GameObject tempFloatingDamage = Instantiate(FloatingDamagePrefab, textLocation, Quaternion.identity) as GameObject;
tempFloatingDamage.GetComponent<FloatingDamageScript>().DisplayDamage(((int)10).ToString());
}
}
}
and:
using UnityEngine;
using System.Collections;
public class FloatingDamageScript : MonoBehaviour
{
public GUIText myGUItext;
public int guiTime = 2;
void update()
{
}
public void DisplayDamage(string damageMessage)
{
Debug.Log("damage message should be " + damageMessage);
myGUItext.text = damageMessage;
// destory after time is up
StartCoroutine(GuiDisplayTimer());
}
IEnumerator GuiDisplayTimer()
{
// Waits an amount of time
yield return new WaitForSeconds(guiTime);
// destory game object
Destroy(gameObject);
}
}
When I run my game and shoot an asteroid, I am given a null reference exception on line 42 of the IsAsteroid script. I need to access the DisplayDamage() function of the instantiated GUIText object that I create on shooting the asteroid. Keep in mind many of these clones can be in existence at once, so it needs to be specific. The GUIText object is being instantiated just fine. I just can’t call a function from it. Any help would be greatly appreciated.