- Home /
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.
Your answer
Follow this Question
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