Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 har16o · Sep 16, 2021 at 12:22 PM · variablesgameobjects

same script on multiple objects access to different variables from another script

I have a player script, which has several variables and slider script, which i use on several sliders. i want to make different sliders (controled by same universal slider script) change differnt variables from player script. For example, O2Slider controls player's consumedO2 and FoodSlider controls player's consumedFood.

Here is myColonistController , which i use on one gameObject - player

public class ColonistController : MonoBehaviour

 { 
     protected float colonistStressLevel;
 
     protected int consumedO2;
     public int ConsumedO2 { get { return consumedO2;  } set { consumedO2 = value;  } }
 
     protected int consumedFood;
     public int ConsumedFood { get { return consumedFood; } set { consumedFood = value; } }
     
     
 
     // Update is called once per frame
     void Update()
     {
         SetColonistCurrentStressLevel();
     }
 
    
     public void SetColonistCurrentStressLevel()
     {
         colonistStressLevel = (consumedO2 + consumedFood) / 10;
     }
 }
 


And here is my ColonistConsumedResourceBarController script, which i use on several gameObjects (sliders)

 public class ColonistConsumedResourceBarController : MonoBehaviour
 {
     [SerializeField]
     protected ColonistController colonistController;
 
     [SerializeField]
     protected Text consumedResourceCounter;
 
     public Slider consumedBarSlider;
     public Gradient consumedBarGradient;
     public Image consumedBarFill;
 
     protected int consumedBarCurrentValue;
 
     // Update is called once per frame
     void Update()
     {
         if (consumedBarSlider.value != consumedBarCurrentValue)
         {
             SetCurrentConsumedBarValue();
         }
     }
 
 
     public void SetCurrentConsumedBarValue()
     {
         consumedBarCurrentValue = (int)consumedBarSlider.value;
         consumedResourceCounter.text = consumedBarSlider.value.ToString();
         consumedBarFill.color = consumedBarGradient.Evaluate(consumedBarSlider.normalizedValue);
     }
 }


So i added ColonistConsumedResourceBarController on two game objects (O2Slider and FoodSlider) but i want them to change (control) different variables from ColonistController. When i change O2Slider's value - ColonistController's consumedO2 changes and when i change FoodSlider's value - ColonistController's consumedFood changes. GameObject with ColonistController i added as reference on slider gameObjects.

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

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

127 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers

How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers

Accesing variable from another class for instantiated gameobjects 0 Answers

Changing variable on diffirent gameobject than that the running script is attached to. 1 Answer

How make the same variable have different values for each object ? 1 Answer


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