- Home /
Variable not changing when you get a reference from another script
My problem is that When I want to get a variable from another script, it's accessing it but the value of it it's not changing.
public class MenuScript : MonoBehaviour {
public int newLevel ;
void OnGUI()
{
GUI.Box (new Rect (10, 10, 100, 120), "Loader Menu");
if(GUI.RepeatButton(new Rect(20,40,80,20), "Level1"))
{
newLevel = 1;
}
if(GUI.Button(new Rect(20,70, 80, 20), "Level2"))
{
newLevel = 2;print("Level " + newLevel);
}
if(GUI.Button(new Rect(20,100, 80,20), "Level3"))
{
newLevel = 3;
}
}
}
public class XMLParserScript : MonoBehaviour {
public GameObject myObject;
public bool newLevel;
void Start()
{
newLevel = false;
objectScript = myObject.GetComponent <MenuScript>();
}
void Update ()
{
LevelList (objectScript.newLevel, newLevel);
}
public void LevelList(int message, bool newLevel)
{
switch(message)
{
case 1:
newLevel = true;
break;
case 2:
NewLevel = true;;
break;
case 3:
newLevel = true;
break;
default:
break;
}
}
I want to access the newLevel Bool variable from XML Parser with another script.
public class AutoPositionItems : MonoBehaviour {
public XMLParserScript objectScript; //Reference to the GemScript
public GameObject myObject; //Reference to the Gems Prefab
void Start()
{
objectScript = myObject.GetComponent <XMLParserScript>();
}
void Update ()
{
if(objectScript.newLevel)
{
//dO SOMETHING
}
else
{
//do Something else
}
}
}
The problem is that when I am pressing one of the GuiButton the newLevel boollean is switching to true and right after that it's switching back to false. Any idea why ?
I think you didn't post all of your code. The problem might be in some of the unposted code. Also, I think there's a problem with your logic, because once you click a button, the "newLevel" bool will forever be true. The switch back to false might be in your unposted code, or is this on purpose (can't imagine why)?
Hot Tip: use debug statements on each part where your "boolean" is refrenced for example
void Start() {
myBoolVar = myObject.SomeFunction();
Debug.Log("Start... my bool is" + myBoolVar.ToString());
}
this tells you where the bool is assigned... and the result of that function.... as soon as you see it set back to false in one of your function .. .then that is the function you work from to sort it out.
Honestly I do not know where the bool is being set to false, the only reason i can think of is Start is being called twice when you load into a new scene causing it to reset. but i can't see any DontDestryOnLoad functions or Sceneloading logic... post every piece of code that uses that boolean.
This is it, only this scripts are using the script.
I am not reloading any scene, I am getting data from an X$$anonymous$$L and I just update everything I have on my screen with new data.
It might be a problem if I have more than one Start function,
I mean 1 Start for each script?
No, that's normal, it's not that. I'm sure there are some lines you omited, for example, in the second script, objectScript is not a global variable, so it would never compile like this. Besides, there is no code that turns "newLevel" to false, appart from the code in the start function, and that's only executed once.
I might be missing something, but it's my hunch that there are lines you did not post, probably in order to make the code smaller and easier for us.
I don't see the point of more the one Start function per class... let alone see how the compiler would even let you do it... unless you are referring to my example? that's an example so yeah...
but have you tried the debug log statements?
public class $$anonymous$$enuScript : $$anonymous$$onoBehaviour {
public int newLevel ;
void OnGUI()
{
GUI.Box (new Rect (10, 10, 100, 120), "Loader $$anonymous$$enu");
if(GUI.RepeatButton(new Rect(20,40,80,20), "Level1"))
{
newLevel = 1;
}
if(GUI.Button(new Rect(20,70, 80, 20), "Level2"))
{
newLevel = 2;print("Level " + newLevel);
}
if(GUI.Button(new Rect(20,100, 80,20), "Level3"))
{
newLevel = 3;
}
}
}
public class X$$anonymous$$LParserScript : $$anonymous$$onoBehaviour {
public GameObject myObject;
public bool newLevel;
void Start()
{
newLevel = false;
objectScript = myObject.GetComponent <$$anonymous$$enuScript>();
print("Start has been called, newLevel is now false");
}
void Update ()
{
LevelList (objectScript.newLevel, newLevel);
print(newLevel);
}
public void LevelList(int message, bool newLevel)
{
switch(message)
{
case 1:
newLevel = true;
break;
case 2:
newLevel = true;;
break;
case 3:
newLevel = true;
break;
default:
print(newLevel);
break;
}
}
Answer by Tehnique · Jul 15, 2014 at 09:49 AM
Ok, I might have an idea what's wrong:
void Update ()
{
LevelList (objectScript.newLevel, newLevel);
}
In the code above you give newLevel as a parameter, even though it's defined as a member of the class you are in. That's a bad idea, because you don't actually work with the nelLevel from the class, but with a copy of it that is lost when the function ends.
The fix is simple and also a good practice:
Don't give any parameters to your LevelList function, because you alredy have them in the class where you work.
So something like this:
public class XMLParserScript : MonoBehaviour {
public GameObject myObject;
public bool newLevel;
public MenuScript objectScript;
//...
And then, in the Update:
void Update ()
{
LevelList ();
}
In the LevelList():
public void LevelList()
{
switch(objectScript.newLevel)
{
case 1:
newLevel = true;
break;
case 2:
newLevel = true;;
break;
case 3:
newLevel = true;
break;
default:
break;
}
}
Try it, I'm curious if that was the problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Script not responding to public variable change 1 Answer
Simple Script getting Errors. 1 Answer
C# Boolean Doesn't Change Value 1 Answer