Change of static variable
Hello. I have a little problem, ive got a script, lets say Script_1, in which there is a while loop, that is running as long as the player is alive. I am accessing bool variable for the while loop from another script, Script_2. In Script_2, in which the variable is declared, i need to change the value of the variable on impact, i.e. the player dies on impact.
As i have found on other threads, i can access the variable, if it is public static bool variable.
And in deed, i accessed this variable in the Script_1 like this:
//Script_2
public static bool alive = true;
//Script_1
bool playeralive = Script_2.alive;
The problem is, that when there is an actual impact, and i have in Script_2:
void OnTriggerEnter(Collider other)
{
if (other.tag == "Asteroid")
alive = false;
}
The variable has not changed, because if it did, the game would end itself. This is the loop in Script_1:
bool playeralive = Script_2.alive;
while (playeralive)
{
for (int i = 0; i < hazardcount; i++)
{
Vector3 spawnpos = new Vector3(sth,sth,sth);
Quaternion spawnrot = Quaternion.identity;
Instantiate(hazard, spawnpos, spawnrot);
}
hazardcount = hazardcount + 5;
}
Application.Quit();
I am very sorry, if this is completely wrong, just started a few days ago. I would appreciate any advice, thanks.
You must do while (Script_2.alive)
ins$$anonymous$$d of while (playeralive)
Answer by GianBert · Sep 25, 2017 at 03:19 PM
You must write in script 1
public bool playeralive;
Script_2 script_2;
void Awake()
{
script_2 = GetComponent<Script_2>();
}
and after you can write
void Update()
{
if(script_2.alive == true)
{
playeralive = true;
}
else
{
playeralive = false;
}
}
Your answer
Follow this Question
Related Questions
Calling variables from other script 1 Answer
Why UNITY Hates Me By Reading Array's Length! 1 Answer
how to controll a variable of a script from another script 2 Answers
How to get all the variables in a folder. C# 0 Answers
How to send a variable from one script to another when the script finishes. 0 Answers