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 Zyck · Dec 25, 2014 at 06:48 PM · parentreferencehierarchysingleton

How to have an instance only reference its parent

I'm trying to make a text box display the value of it's parent objects stats. The text box is attached to a sprite which has an attached script which tracks the stats of the sprite such as HP, damage, etc.

I created a standard singleton (as shown here) in the stat tracking script and reference it in the text box script to show the value of the sprite. This part works fine. However, when I create another sprite from the same prefab, both text boxes show the same value instead of only showing the value of its parent.

So if I have two sprites, one with 10 hp and one with 20 hp, both text boxes show 10hp or 20hp, when one should show 10 and one should show 20. How do I set the text box to only show the value of its parent? Can I still use a singleton for this or do I need another method?

I should also add that I've tried doing this without singletons and so far without any success.

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 hav_ngs_ru · Dec 26, 2014 at 01:52 PM 0
Share

show us your code pls

1 Reply

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

Answer by Mmmpies · Dec 26, 2014 at 04:40 PM

Untested but when you add a child object to a parent its assigned a numbe child 0, child 1 and so on.

Now we can't see how you're storing the hp number but I'll guess a script called HP that holds a currentHP variable, if so and if the object your want to get the information from was the first child added then something like should work.

 Text.text = parent.GetChild(0).GetComponent<HP>().currentHP.ToString();

This is in c#

If the sprite with the script attached is in the child added second change the 0 to 1.

Hope that makes sense but difficult to fit in with your code as you haven't given us an example.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

moving child objects in hierarchy and keep stacks/sorting? Help needed 0 Answers

Change an object's grandparent 2 Answers

How do I fix this error when trying to use the Watson SDK for Speech to Text? 0 Answers

Create reference to Hierarchy objects that persists between different scene loads 2 Answers

Moving objects together without parenting 5 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