Using Buttons to set Setters not working?

I made a new project for this demo. I created 2 Buttons and 2 c# scripts. When I set them inside the method it works, but when I access them outside the method, the value gets reset to 1. Here is my code.

using UnityEngine;
using System.Collections;

public class xtest : MonoBehaviour{

	int points1 = 1;
	int points2 = 1;

	public int Points1 {
		get{ return points1; }
		set{ points1 = value; }
	}

	public int Points2 {
		get{ return points2; }
		set{ points2 = value; }
	}
}

//Next script is here//

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class xtestButton : MonoBehaviour
{
	xtest button;
	Text t;

	void Start ()
	{
		button = gameObject.AddComponent<xtest> ();
		t = GameObject.Find ("test").GetComponent<Text> ();
	}
	
	void Update ()
	{
		print(button.Points1.ToString() + " - " + button.Points2.ToString());
	}

	public void Button1 (int button1)
	{
		button.Points1 = button1;
		t.text = button.Points1.ToString() + " - " + button.Points2.ToString();
	}

	public void Button2 (int button2)
	{
		button.Points2 = button2;
		t.text = button.Points1.ToString() + " - " + button.Points2.ToString();
	}
}

Results;
I press button named 10, I get “10 - 1” so far so good.
I press button named 20, I get “1 - 20” not so good. It should read 10-20.
I press button named 10, I get “10 - 1” also not correct, should read 10-20 since both buttons now were set.

In the update function I get simultaneous (1-1 and 10-1), or (20-1 and 1-1) depending on which button pressed. What is going on here? I told it to print 2 values I get 4 printed? It prints the setted values and the original values at the same time. huh?

Here is a pict of my button containing the script. There are no other codes or anything else involved. It’s a clean project.

You should have mentioned your other question as it seems to be the same problem but this time you finally provided enough information.

Your problem is that it seems you have lost track of where which instances of what classes are actually used.

To me your setup looks like this:

  • You have two gameobjects “Button1” and “Button2”.
  • Each of those buttons has an UI.Button script and a xtestButton script attached

The problem here is that the xtestButton script adds an instance of the “xtest” script to the gameobject it’s attached to. Since you have two xtestButton instances, each gameobject (Button1 and Button2) will have it’s own “xtest” instance as well.

Since you setup the UI.Button script of “Button1” to invoke the “Button1()” method of it’s xtestButton script instance and the Button script on “Button2” to invoke the “Button2()” method of the second xtestButton instance on the Button2 gameobject, each button works on completely seperate instances.

Two solutions:

  1. Remove the “xtestbutton” instances from both buttons. Add your “xtestbutton” script to a seperate gameobject and drag that gameobject in the “object reference field” of your onclick handler on each button. That field is below the “Runtime Only” dropdown field. Now you can select your desired method you want to run. If you do that on both buttons, both will actually work with the same instance of your xtestButton script.
  2. Keep the xtestButton on each button, but remove the AddComponent call from Start. Instead add the “xtest” component manually to a seperate gameobject. Change the “button” variable in your xtestbutton script from private to public. Now you can simply drag the gameobject with your xtest script onto that variable on each button. In this solution each button still has it’s own xtestbutton script instance, but both work on the same “xtest” instance.

ps: class names should use UpperCamelCase.