Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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
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 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
2

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

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

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to display text on a gamebject? 3 Answers

Access specific Text among multiple children 0 Answers

Using a lot of calls and if else statement, is there a better way to do this? 1 Answer

Load a GameObject that is outside of script and set it active at the same time 1 Answer


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