Hey guys!
I’m trying to do an array in my game buuut, it doesn’t work and i don’t know why
I want to hide a GameObject (Life Icons) when my character’s get hit.
I appreciate if you help me.
“NullReferenceException: Object reference not set to an instance of an object
Player.OnTriggerEnter (UnityEngine.Collider otherObject) (at Assets/scripts/Shooter/Player.cs:63)”
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float PlayerSpeed;
public GameObject BolinhaPrefab;
public GameObject ExplosaoPrefab;
public static int Score = 0;
public static int Lives = 5;
public GameObject [] GuiLives1;
public static int Missed = 0;
// Update is called once per frame
void Update ()
{
//Velocidade de movimento
float antToMove = Input.GetAxis("Horizontal") * PlayerSpeed * Time.deltaTime;
//Mover o Bob
transform.Translate(Vector3.right * antToMove);
// Destruir ao sair da tela
if (transform.position.x <= -7.6f)
transform.position = new Vector3 (7.4f, transform.position.y, transform.position.z);
else if (transform.position.x >= 7.6f)
transform.position = new Vector3 (-7.4f, transform.position.y, transform.position.z);
if (Input.GetKeyDown("space"))
{
//Disparar Bolinha
Vector3 position = new Vector3 (transform.position.x, transform.position.y + (transform.localScale.y / 2));
Instantiate(BolinhaPrefab, position, Quaternion.identity);
}
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 120, 20), "Score: " + Player.Score.ToString());
GUI.Label(new Rect(10, 30, 60, 20), "Vidas: " + Player.Lives.ToString());
GUI.Label(new Rect(10, 50, 120, 20), "Missed: " + Player.Missed.ToString());
}
void OnTriggerEnter (Collider otherObject)
{
if (otherObject.tag == "enemy")
{
Player.Lives--;
Inimigo enemy = (Inimigo)otherObject.gameObject.GetComponent("Inimigo");
enemy.SetPositionAndSpeed();
StartCoroutine(MatarBob());
GuiLives1 = new GameObject[5];
for (int i = 0; i < 5; i++){
GuiLives1[i].gameObject.SetActive(false);
}
}
}
//Bob Morre aqui
IEnumerator MatarBob()
{
Instantiate(ExplosaoPrefab, transform.position, Quaternion.identity);
gameObject.renderer.enabled = false;
transform.position = new Vector3(0f, transform.position.y, transform.position.z);
yield return new WaitForSeconds(1.5f);
if (Player.Lives > 0)
gameObject.renderer.enabled = true;
else
Application.LoadLevel(3);
}
}