0

enter image description here

as you can see, I have player1, I have added a health canvas, with a red background and green foreground, I need to know how to access that in the script, what code I use in the script to access the green foreground's rect transform to decrease it and show more of the red when health goes down.

Any help is appreciated...

thanks

GarudaAiacos
  • 161
  • 2
  • 17
  • What does your script look like so far? What have you tried? – Serlite Oct 04 '16 at 20:01
  • I have a standard script that holds the health and brings it down when the player gets hit and then game over when they die, i just dont know how to reference that canvas / UI object in the script or access its rect transform ive been trying to look up online how to reference it in the script to no avail so I havent tried anything yet... – GarudaAiacos Oct 04 '16 at 20:02
  • Saw one thing that said I needed to put this0 – GarudaAiacos Oct 04 '16 at 20:02
  • public RectTransform healthBar; -- but thats not right as it didnt work and dont see how that would reference it.. – GarudaAiacos Oct 04 '16 at 20:03
  • Possible duplicate of [Accessing a variable from another script C#](http://stackoverflow.com/questions/25930919/accessing-a-variable-from-another-script-c-sharp) – Serlite Oct 04 '16 at 20:04
  • Using a public reference is indeed the easiest way to go - in the inspector, drag that object onto the public field of the script. You can then access the properties/methods of the given component in your code. – Serlite Oct 04 '16 at 20:05
  • im not trying to access a variable of another script, im trying to access a game UI object and change the rect transform of it in the script – GarudaAiacos Oct 04 '16 at 20:05
  • Getting a reference to a component is the same approach as referencing a script. In this case, you'll just be accessing the properties of the component rather than a script. – Serlite Oct 04 '16 at 20:06
  • when i try to drag the health bar or the image over to the inspector with the script selected it doesnt let me drag anything to it? – GarudaAiacos Oct 04 '16 at 20:09
  • What data type is your public field? Make sure it matches what you're trying to access, and make sure you don't have any script errors as this will prevent the inspector from updating the script's public fields. – Serlite Oct 04 '16 at 20:11
  • what data type do I need for my public field, I am trying to access a UI Image component Rect Transform, which is a child of a canvas, that is the question I believe I am asking, just public Image and access the Rect Transform after I drag it? – GarudaAiacos Oct 04 '16 at 20:12
  • You can create a variable of type `RectTransform` in your script, and drag the image's `RectTransform` onto the public field in the inspector. Alternatively, you can cast a `Transform` to a `RectTransform`, or use `GetComponent()` on a reference to the image's GameObject. – Serlite Oct 04 '16 at 20:22
  • Do you have the 'using UnityEngine.UI' statement at the top of your class? With out that you will not add the 'RectTransform' variable. Also have you considered using a Slider to do this? Near the end of this video they have some example of using it as a health bar: https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-slider – padonald Oct 05 '16 at 15:23

0 Answers0