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 jrauden · May 09, 2017 at 05:47 AM · scripting problem

typecasting GameObject & UI slider

I have searched everywhere for the answer to this and can't find it (in c#), but i know this is super simple:

I have a UI slider that i am using as a health bar, i took alot of the code from the Unity Survival Shooter tutorial. My problem is this: In the tutorial the scene starts with a player already in the hierarchy, so all they had to do to apply the slider to their player.health was drag and drop in the editor.
In my game the player is instantiated by a GameManager after initialization so i can't just drag and drop the slider using the editor.

In my script i have a declaration of: public Slider healthSlider; Trying to get to that Slider through my script is proving to be way harder than i know it should be.
I tried using: healthSlider = GameObject.FindGameObjectWithTag ("healthslider"); this won't work because i can't convert GameObject to Slider. So I tried: healthSlider = (Slider) GameObject.FindGameObjectWithTag ("healthslider"); (trying to find the slider as a GameObject and typecast it to a slider, same problem basically.

Now I just can't think of what else to try. All I'm trying to do is get a reference to that Slider. Currently it is a child of a UI>Canvas, does that matter?

Thank you in advance for your help, i know this is an easy one. :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ruffer07 · Dec 23, 2017 at 07:12 PM

I'm not sure if you are still struggling with this, but I just hit this problem with my custom editor for an audio/music player script I'm currently in the process of making.

Try casting to (UnityEngine.UI.Slider) instead of (Slider) .

So

 healthSlider = (UnityEngine.UI.Slider) GameObject.FindGameObjectWithTag ("healthslider");
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

transform/rotate/scale tool callback for drag finished? 0 Answers

How to make W button move towards mouse direction 1 Answer

Cannot access another script's Vector3? 1 Answer

On/Off Trigger is making Light Flicker 1 Answer

Cinemachine errors on Unity 2018.2.0f2 0 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