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 /
  • Help Room /
avatar image
0
Question by PandaWrangler · Mar 25, 2017 at 05:09 PM · gameobjectslideraccessthis

Updating GameObject Variables in Script

I've done a lot of searching on this topic and can't find the answer to my question. It may be something obvious but I am relatively new to Unity, coming from a strong background in other programming languages/environments.

This issue I have is how do you modify or work with a game object's variables or functions through the script that is attached to it. I.E. lets say I have a music volume slider. I know that if I want to update the stored volume value I can do the following:

 private void Update ()
     {
         GameManager.Instance.music_volume = music_slider.value;
     }

The above works fine for storing the value from the slider into my game manager singleton class which then updates the user player preferences. This issue I can't wrap my head around is how do I get "music_slider" to reference the Slider Game Object that the script is attached to.

This has been my solution so far but it just doesn't feel right, any suggestions?

 private Slider music_slider;    // Internal Slider Reference

 private void Awake()
 {
     // Internal slider reference is always to the Slider Game Object that the script is attached to.
     music_slider = this.gameObject.GetComponent<Slider>();
 }


For complete clarity here is the full structure of my script class that is attached to a MusicVolumeSlider

 public class MusicSliderControl : MonoBehaviour
 {
     private Slider music_slider;    // Internal Slider Reference

     private void Awake()
     {
         // Internal slider reference is always to the Slider Game Object that the script is attached to.
         music_slider = this.gameObject.GetComponent<Slider>();
     }

     // Use this for initialization
     private void Start ()
     {
         music_slider.value = GameManager.Instance.music_volume;
     }
 
     // Update is called once per frames
     private void Update ()
     {
         GameManager.Instance.music_volume = music_slider.value;
     }
 }
Comment
Add comment · Show 5
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 Namey5 · Mar 25, 2017 at 10:14 PM 1
Share

GetComponent is how you access components when you know the object they're attached to. So in this case, if the slider is attached to the same object as this script, all you need to do is;

 music_slider = GetComponent<Slider>();
avatar image PandaWrangler Namey5 · Mar 27, 2017 at 01:53 AM 0
Share

Ah okay so I don't need "this.gameObject" in order to use "GetComponent". By calling "GetComponent<>()" in the script it will automatically reference the game object that the script is attached to?

avatar image Namey5 PandaWrangler · Mar 27, 2017 at 04:47 AM 1
Share

That is correct.

Show more comments

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

115 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

Related Questions

Minesweeper - Surrounding Objects - C# 0 Answers

this.GetComponent() won't return gameObject? How to get it without using this.gameObject? 3 Answers

Find a specific object using transform 0 Answers

Why Does Prefab Loose UI elements on Scene Change 0 Answers

I have searched like the whole Internet for this lol (get Value of specific Prefab(clone)) 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