• 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 doomprodigy · Jan 22, 2012 at 12:00 PM · c#instantiateguitext

GUIText Instantiation and Update the Instantiated Text. C#

Hello,

What I am trying to do is instantiate the damage you do on an enemy and render the damage you did on the text that is instantiated. Here is the script I am working on. I have tried multiple methods that I thought might work, but thus far no success. Unity Answer folk to the rescue?

 private GUIText GUIDamage;
 public GameObject GUIPrefab;
 
 void Damage ( float damage  ){
       GameObject GUIDamage = Instantiate(GUIPrefab, Camera.main.WorldToViewportPoint(gameObject.transform.position), Quaternion.identity);
     GUIDamage.guiText = Shooting.damageToSend;
     }

Currently I have gotten various errors with various methods of trying to get it to work. I have seen a similar script on Unity Answers about t$$anonymous$$s, but it was in that nasty Unityscript. :O

The current error I recieve is : " Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)" I t$$anonymous$$nk I know why I get t$$anonymous$$s, I just don't know how to get the code to work without errors haha.

Cheers, Thank you for your time.

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
3
Best Answer

Answer by syclamoth · Jan 22, 2012 at 01:59 PM

The problem is that you are trying to define GUIDamage twice, as different types!

First, here-

 private GUIText GUIDamage;

then, here-

 GameObject GUIDamage

Not only will t$$anonymous$$s cause issues with one $$anonymous$$ding the other, I doubt that that is actually what you want.

In t$$anonymous$$s case, you should stop trying to re-define GUIDamage. It's defined correctly the first time, there's no need to define it again (and it would disappear at the end of the scope if you did anyway).

Instead, use GetComponent to grab the GUIText out of the newly instantiate object, and then you can refer to GUIDamage in Update to modify the text!

 GUIDamage = ((GameObject)Instantiate(GUIPrefab, Camera.main.WorldToViewportPoint(gameObject.transform.position), Quaternion.identity)).GetComponent<GUIText>();
Comment
Add comment · Show 3 · 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 doomprodigy · Jan 22, 2012 at 02:12 PM 0
Share

Got it working. Looks like you saved my ass again :P

avatar image tapticc · Mar 03, 2014 at 05:21 PM 0
Share

Would love to thumbs up but I'm new, I wanted to show points on screen when an asteroid was blown up and couldn't find an example of how to position it correctly so answered multiple questions with this answer!

avatar image D_MC · Apr 05, 2014 at 09:02 AM 0
Share

Hi guys,

I've finished going through the Space Shooter/Asteroids tutorial and now I am trying to tweak the game to keep learning.

I am trying to do what tapticc mentioned above and have the points for a destroyed asteroid flash up on the screen at the location of the exploding asteroid. I have played around with the above code but with no luck.

Would it be possible to get a larger code snippet that shows how this should work?

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

7 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

C#, How do I make the GUIText Show up if Instantiate from a prefab. 1 Answer

C# 2d Instantiate GUIText and lock it to position on map, not to follow camera. 1 Answer

Multiple Cars not working 1 Answer

GUIText Instantiation to show at Asteroid position 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