- Home /
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.
Answer by Bunny83 · Jun 16, 2015 at 10:35 AM
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:
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.
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.
Hey thanks man, that helped a lot. Changing scenes, the gameObject would no longer be available, does that mean I will lose all my setters value? Or is there a way I can transfer the same gameObject to the next scene, so I can keep the values? Like moving from the main menu scene to the game level 1 scene.
@d112570:
Sure, that's what DontDestroyOnLoad is good for. An object that has been "marked" with that method will not get destroyed when a new scene loads. However keep in $$anonymous$$d that if that object is in say "Scene1" and you somehow return to that scene you will end up with two instances. That's because when you load the scene again it again contains that gameobject. So each time you load that scene again you get another instance.
Also note that DontDestroyOnLoad only workd on top-level gameobjects. If you mark a child object with that method but not it's parent, the object will get deleted at a scene change since the parent is destroyed and with it all childs.
The best approach is to have an additional loading scene which you only load once when the application starts and you never (ever) load the scene again.
Of course links to that object has to be established manually via code (FindObjectOfType / GameObject.Find + GetComponent / Singleton).
Another problem is when I create a new script and get the setters my results are are defaults of 0. Script a is my getter and setter script, script b is to set my getters, script c is to get my setters.
I can call my getters and return my values in script b, when I use script c I get its default values. Script b and c is on my parent game object. Anyway to resolve.
Your answer

Follow this Question
Related Questions
Accessing class properties through getters & setters in editor mode 1 Answer
Javascript : Get variable & run a method from another script 0 Answers
getter and setter not working 1 Answer
Is it possible to change the "jump" positive button during runtime? 1 Answer
Proper use of c# properties? 1 Answer