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 Reien · Oct 01, 2017 at 08:36 AM · scripting problemimagelinking

Is there any way to link an image object to another game object's component?

I'm not sure if I worded it right, but what I want to ask is for example

I have an Image object in the heirarchy that I want to link to my instantiated object (or player), how do I link it to the player's script?alt text

I wanted to link the damage image I have in the heirarchy to the Damage Image I have on this script but I don't know how to do it in script. I know that I could just drag it there if the object is also in the scene, but when I try to instantiate the same object from the assets (as a prefab) all those sliders and images are empty. I tried linking it by using these set of codes:

    healthSlider = (Slider)FindObjectOfType (typeof(Slider));
 //damageImage =
    m_FillImage = (Image)FindObjectOfType (typeof(Image));

but when I write the same for the damageImage, it also automatically takes the Image that is set to the FillImage. This is how the UI objects look like in the heirarchy:

alt text

I'm guessing my code in the script is getting the last image object in the canvas, but I have no idea how to call the specific image object.

I'm sorry for the long question I just wanted to be as specific and accurate as possible with my question, because I haven't seen any answers for this on any other questions. Or I just don't know the right terms to search for. And I'm new to asking questions so it's really long. Thanks in advance

fill.png (3.0 kB)
health.png (11.0 kB)
Comment
Add comment · Show 3
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 Positive7 · Oct 01, 2017 at 09:47 AM 0
Share

I'm not sure if i get this right It depends which object your script is attached to. If it is attached to DamageImage then simply use GetComponent<Image>().sprite;

if attached to a child of HUDCanvas then use transform.Find

  m_FillImage = transform.parent.Find("DamageImage").GetComponent<Image>().sprite;

transform.Find will find a child by name so use parent which will be HUDCanvas

avatar image Reien Positive7 · Oct 02, 2017 at 11:01 AM 0
Share

The PlayerHealth script is attached to a player object that I'm instantiating at the start of the scene. The DamageImage is attached to an object that can damage the player object.

I'll try that and see if it would work. Thanks

avatar image Reien Positive7 · Oct 02, 2017 at 11:19 AM 0
Share

I actually did it by manipulating that line of code a bit with

 damageImage = (Image)GameObject.Find("DamageImage").GetComponent<Image>();

Though I'm not sure if that's really optimized. I do want to know more ways to do it

0 Replies

· Add your reply
  • Sort: 

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

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

Image Stitching without external libraries 0 Answers

WWW image method to change raw image texture. Script problem. 0 Answers

How to rotate GUI image on cursor drag? 0 Answers

AR Foundation Image Tracking and Markers 0 Answers

Reusable Canvas for enlarging images 2 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