Changing script variable from another script doesn't change it in the original script?
I have made a script (lets call it Script1), which accesses a variable from another script (Script2) and modifies it. Script2 then displays it on screen.
Script1 successfully finds the variable and modifies it (checked it with the print function and it works), however when Script2 displays it on screen it shows the unmodified version.
Script1
private void UpdateCold() {
GameObject thePlayer = GameObject.Find("HUDColdText");
HUDControl cold1 = thePlayer.GetComponent<HUDControl>();
if (cold1.cold < 96)
{
cold1.cold += 6;
}
else
{
cold1.cold = 101;
}
print(cold1.cold);
}
Script2 public int cold = 100; void Update () {
if (Time.time > nextActionTime)
{
if(cold > 0)
{
cold--;
}
else
{
cold = 0;
}
nextActionTime += 1;
coldText.text = "Cold: " + cold + "/100";
}
}
The weird part is that I have already built the game a couple of days ago (a prototype version) and it works just fine. But after a few days of testing I came back into unity and it doesn't work anymore. I am really confused about this, so any help is appreciated.
Your answer
Follow this Question
Related Questions
NullReferenceException on getComponent 1 Answer
C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer
How to alter Right/Top Value from RectTransform 0 Answers
Can jump even when in the air. 0 Answers
C# > Camera tagged MainCamera > GetComponent<[ScriptClassHere]>() returns null ? 0 Answers