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 Sean_Casey · Aug 13, 2014 at 05:12 PM · camera viewportfloating-text

Floating Damage Text Above Object on Hit

When my asteroids are hit by a bullet, I would like to show the damage done as a GUIText popping up on top of them. I have a script for the asteroids, where the collisions are detected and and an instance of the GUIText is called. This works fine, as far as I know. I can make it pop up in the center of the screen if I set the tempFloatingDamage instantiate coords to Vector3(0.5f,0.5f,0). But when I try to calculate the position of the asteroid and spawn it there, I get "NullReferenceException: Object reference not set to an instance of an object". I'm not properly grabbing the transform.position for that particular instance of the asteroid. It should be simple, but my brain just won't wrap around it. Here is my code:

 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 (other.tag == "Bullet")
         {
 
             health = health - 10;
             Vector3 textLocation = Camera.main.WorldToScreenPoint(transform.position);
             textLocation.x /= Screen.width;
             textLocation.y /= Screen.height;
             Transform tempFloatingDamage = (Transform)Instantiate(FloatingDamagePrefab, textLocation, Quaternion.identity);
             tempFloatingDamage.GetComponent<FloatingDamageScript>().DisplayDamage(((int)10).ToString());
         }
 
 
         if (explosion != null)
         {
             Instantiate(explosion,transform.position,transform.rotation); //Create a new explosion instance when hit
         }
 
 
     }
 }

This is the line giving me trouble: Vector3 textLocation = Camera.main.WorldToScreenPoint(transform.position);

Any help would be greatly appreciated. Thank you!

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Aug 13, 2014 at 05:17 PM

I see a problem here, and a potential problem here.

First, you are Instantiating a game object but you are casting the result to a transform. Don't do that. 'FloatingDamagePrefab' and 'tempFloatingDamage' need to be of the same type...they can be either Transform or GameObject. Make the line:

         GameObject tempFloatingDamage = Instantiate(FloatingDamagePrefab, textLocation, Quaternion.identity) as GameObject;

If this does not fix your problem, next make sure that 'FloatingDamagePrefab' has been assigned, and that you don't have another copy of this script somewhere.

Further down the road based on your question, you will want to place your text at the point of damage. Note that GUIText lives in Viewport space, so yo will need to do a Camera.WorldToViewport() conversion on the hit point to get the position.

Comment
Add comment · Show 7 · 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 Sean_Casey · Aug 13, 2014 at 05:28 PM 0
Share

Doing that I get: "error CS0029: Cannot implicitly convert type UnityEngine.Transform' to UnityEngine.GameObject'" FloatingDamagePrefab has been assigned and there is only 1 copy of the script. The point of this code WAS to get the text at the point of damage. If I delete lines 34, 35, and 36, and if on line 37 where it says "textLocation" I change it to "new Vector3(0.5f,0.5f,0)", I will get the text appearing in the center of the screen. So I'm able to get the text to appear. What I need is to put something where "textLocation" is that will make my text appear above my asteroid. Does that all make sense?

avatar image robertbu · Aug 13, 2014 at 06:01 PM 0
Share

Oops. It should have been 'as GameObject;' I fixed the line. As for your appearing above the hit point, change lines 34 - 36 to:

 Vector3 textLocation = Camera.main.WorldToViewportPoint(transform.position);
 textLocation.y += 0.05f;

Adjust '0.05' as appropriate. It represents a fraction of the screen height. Also pay attention to where you've set your anchor in your GUIText.

avatar image Sean_Casey · Aug 13, 2014 at 11:19 PM 0
Share

Thanks for all the help so far. I really appreciate it! I changed what you suggested, but line 34 sends out a null reference exception error when I shoot the asteroid. No text appears. Any thoughts?

avatar image robertbu · Aug 14, 2014 at 12:03 AM 0
Share

Accessing Camera.main will generate a null reference exception if you've changed the tag on the main camera. The tag needs to be '$$anonymous$$ainCamera'. Or alternately you can substitute some other camera reference ins$$anonymous$$d of 'Camera.main'. For the conversion to work, the camera used needs to be the one the user is using to view the hit.

avatar image Sean_Casey · Aug 14, 2014 at 12:29 AM 0
Share

Dear sweet jesus... I spent HOURS on this, and that was my mistake? The damn camera tag? UGGGGG! Thank you so very much, Robert. I'd have wasted another entire day on this had you not told me that. Thanks for being so perceptive, patient, and helpful. You're the best!

Show more comments

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

2 People are following this question.

avatar image avatar image

Related Questions

Collisions between clones, text/numbers, spawn control. 2 Answers

How to expand camera culling boundaries 1 Answer

Change camera viewport rect via script 1 Answer

Limit object movement within camera view 1 Answer

Spit screen cameras not working right 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