• 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
Question by lucasjofre · Jan 02, 2018 at 06:53 PM · gameobjecttextspace shooterattachment

Problems attaching a GameObject in a script

Hello Everyone

I'm new at Unity and programming in C#, so this might be a trivial question. Right now I'm making the Space Shooter game of the tutorial and I need to display the score value in the scene. I created an UI Text Game Object and correctly placed at the left top side corner of the screen. Then, I made some changes in the script that controls the explosions and destroys the objects, in order to show the score correctly. So I created a "public Text scoreText", to show in the inspector. The problem is that I can't pull and drag the Text Game Object to the "Score Text" property of the script. Whenever I try to do it, my cursor indicates that it can't do it. Is there any other way can do that?alt text

problem.png (84.1 kB)
Comment

People who like this

0 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 hexagonius · Jan 02, 2018 at 07:49 PM 0
Share

Does the Score Text has a UI Text component on it?

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Lysander · Jan 02, 2018 at 08:37 PM

You cannot drag a scene object into a prefab reference that's not in the scene. If you want the Score Text to be available to all instances of the Asteroid prefab, then it needs to be a part of that prefab, or a part of another prefab that the Asteroid can instantiate when needed.

That's probably not that useful in this case, so you'll more likely want the Score Text and Asteroid to learn about eachother when the Asteroid is actually instantiated in the scene. This means that the Score Text needs to make itself more readily known- maybe by registering itself to a static / singleton Game Manager of some sort, or being a singleton reference itself, etc. This puts it into a position where the Asteroid can easily find and reference it when needed.

I would personally go the other way- UI should know about game data, but game data shouldn't know about UI. There should be a manager of some sort that handles the game score for the player, how many lives the player has, etc... This manager can be static, or it can be a singleton in the scene if you REALLY like GameObjects for some reason, or it can be attached to the player if you really want too. This manager should keep track of the Asteroids destroyed by the player, and increase the score when appropriate, and it should have a UnityEvent or some other kind of broadcast delegate (call it "OnScoreChangeEvent") that fires whenever the score is updated. The UI should register for that event, and receive the signal whenever it's fired, and update its own text to reflect the new score.

That way, the game works without knowing anything about the UI, and the UI can be completely swapped out without having any effect on the game except losing the display.

Comment
OneCept-Games
lucasjofre

People who like this

2 Show 0 · 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

Answer by OneCept-Games · Jan 02, 2018 at 07:49 PM

If your Score GameObject is really a Text type, then it should be possible. So make sure you are dragging a Text type object to this attribute.

Comment

People who like this

0 Show 0 · 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.

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

108 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 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

Instantiate a GameObject at a position specific to an element that was found in a text 1 Answer

Carry over texts from one scene to another? 1 Answer

How do I carry over ui text to the next by obtaining the gameobject children 0 Answers

GameObject text colors in hierarchy panel 1 Answer

Update Text on Gameobject initiated from script from other Gameobject 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