• 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 /
  • Help Room /
avatar image
0
Question by Luis_Hayabusa28 · Jun 08, 2016 at 10:58 PM · nullreferenceexceptionfunctions

Help with my PopUp Damage Text!

Hello, I have a problem with my script to display a text indicating the damage that the player made an enemy , but I checked in and not be wrong . When testing the script I get the error " NullReferenceException : Object reference not set to an instance of an object" . The scripts are as follows :

Health: This script is attached to the player and enemies

 public int HP = 5;
     public int HPMaximo = 5;
     public GameObject PrefabTextoDamagePopUp;
     public List<GameObject> TextoDamagePopUpLista = new List<GameObject>();
     int indiceTextoDamagePopUpListaActual = 0;
 
     public void doDamage(int Damage){
         HP -= Damage;
         TextoDamagePopUpLista[indiceTextoDamagePopUpListaActual] = (GameObject)Instantiate
             (PrefabTextoDamagePopUp, 
                 new Vector3
                 (transform.position.x, transform.position.y + 2, transform.position.z),
                 Quaternion.identity);
         indiceTextoDamagePopUpListaActual++;
         TextoDamagePopUpLista[indiceTextoDamagePopUpListaActual].GetComponent<TextDamagePopUp>().DamageMostrado = Damage;
     }


Arma: This script is attached to the weapon of the player

 public Sprite[] SpriteArray;
     public int[] DamageArray;
     public bool ArmaActiva = false;
     public static int ID;
     public static int DamageActual = 2;
 
 
 
     void OnTriggerEnter2D(Collider2D coll){
         if(ArmaActiva && coll.GetComponent<Health>() && coll.tag != "J"){
             coll.GetComponent<Health>().doDamage(DamageActual);
         }
     }
 }

TextDamagePopUp: This script is attached to the 3d text prefab , which is going to show the damage that the player has made

 public float tiempoVida;
     float tiempoVidaA;
     public int DamageMostrado;
     void Start(){
         tiempoVidaA = tiempoVida;
     }
 
     void FixedUpdate(){
         tiempoVida -= Time.fixedDeltaTime;
         GetComponent<TextMesh>().text = System.String.Empty + DamageMostrado;
 
         if(tiempoVida <= 0)
             Destroy(gameObject);
     }
 
 }

If there is another way to show the damage done in a text, they will thank you if I explain it please! alt text

cool-screenshotxddddd.png (50.4 kB)
Comment
Add comment · Show 2
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 TBruce · Jun 08, 2016 at 11:19 PM 0
Share

You need to post the complete class file(s) involved. You also need to post a little more than NullReferenceException. Place a comment in your code where the error occurs like this

 //<---- error here
avatar image Luis_Hayabusa28 TBruce · Jun 08, 2016 at 11:55 PM 0
Share
 NullReferenceException: Object reference not set to an instance of an object
 Health.doDamage (Int32 Damage) (at Assets/Scripts/Health.cs:20)
 Arma.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/Scripts/Arma.cs:18)
 
 The lines of the corresponding error are the following:
 
 Health.cs:20 TextoDamagePopUpLista[indiceTextoDamagePopUpListaActual].GetComponent<TextDamagePopUp>().Damage$$anonymous$$ostrado = Damage;
 
 Arma.cs: 18
 coll.GetComponent<Health>().doDamage(DamageActual);
 


0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[solved] This error "NullReferenceException: Object reference not set to an instance of an object" occurs only the first time? 0 Answers

NullReferenceException, Script Error 1 Answer

GetComponent returns null 1 Answer

NullReferenceException 0 Answers

Reference lost in NetworkEntity 0 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