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 ?

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.