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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

linking scripts 1 Answer

Trouble using WWW.LoadImageIntoTexture 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges