Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ryujose · Aug 17, 2014 at 06:09 AM · gameobjectscoreinstance

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ryujose · Aug 17, 2014 at 03:51 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges