- Home /
NullReferenceException: Object reference not set to an instance of an object C#
NullReferenceException: Object reference not set to an instance of an object SaludEnemigo1.OnTriggerEnter2D (UnityEngine.Collider2D collider) (at Assets/Scripts/SaludEnemigo1.cs:62)
This is my entire error, I don't know what to do. Anybody can help me, please?
Those are my codes.
using UnityEngine;
using System.Collections;
public class ScoreScript : MonoBehaviour {
// Interfaz Grafica
public GUIText scoreText;
private int score;
void Start () {
score = 0;
UpdateScore ();
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
}
using UnityEngine;
using System.Collections;
/// Comportamiento de la salud de los objetos
public class SaludEnemigo1 : MonoBehaviour {
/// Numero de puntos de salud
public int ps = 2;
private GameObject Enemigo1;
private ScoreScript scoreScript;
public int ScoreValue;
/// Es jugador o enemigo?
public bool esEnemigo = true;
// LLamamos el script score y el tag enemigo1
void start ()
{
GameObject scoreScriptObject = GameObject.FindGameObjectWithTag ("ScoreScript");
if (scoreScriptObject != null) {
scoreScript = scoreScriptObject.GetComponent <ScoreScript> ();
}
if (scoreScript == null) {
Debug.Log ("No se puede encontrar ScoreScript");
}
Enemigo1 = GameObject.FindWithTag ("Enemigo1");
if (Enemigo1 == null) {
Debug.Log ("No se encuentra a enemigo1");
}
}
void OnTriggerEnter2D(Collider2D collider)
{
//Es un disparo?
Disparar disparo = collider.gameObject.GetComponent<Disparar>();
if (disparo != null)
{
// Revisamos si es enemigo o compañero
if (disparo.esDisparoEnemigo != esEnemigo)
{
ps -= disparo.daño;
// Destruimos el disparo
// No colocar solo Destroy()
//sino eliminara el Script
Destroy(disparo.gameObject);
if (ps <= 0)
{
// Muerto + EfectoEspecial de particulas, += score
scoreScript.AddScore(ScoreValue);
EfectosEspecialesScript.Instancia.Explosion(transform.position);
EfectosDeSonido.Instancia.ReproducirSonidoExplosion();
Destroy(gameObject);
}
}
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Player")
ps = 0;
EfectosEspecialesScript.Instancia.ExplosionAsteroide(transform.position);
Destroy(Enemigo1);
}
}
I want the line "scoreScript.AddScore(ScoreValue);" to work but I don't know what to do, I've searched for 3 h and nothing. Maybe the error it provide in console is for the method I call the script? If it's that, how can I do the best way for instance?
Thanks a lot for your help!
Thanks for the help, I was turning so crazy looking for the object reference for hours!!! And it was a simple capital letter, omg... Thanks again!
Answer by JustFun · Aug 17, 2014 at 06:32 AM
In
void start ()
Start must begin with capital letter. C# is case-sensitive, so your start() function isn't executed at all.
Answer by robertbu · Aug 17, 2014 at 06:34 AM
for JustFun...and since start is not being called, your variables are not being initialized.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How To Stop Destroyer From Destroying? 2 Answers
Detection if a GameObject is below you or next to you? 1 Answer
Unity3D Pressure Plate request. 3 Answers
Add force to GameObject 2 Answers