- Home /
Getter Working, Setter not.
using UnityEngine;
using System.Collections;
public class BaseSkills : MonoBehaviour
{
int strBase = 1;
public int Strength {
get{ return strBase; }
set{ strBase = value; }
}
Top is my getter and setter code. Where Strength base value is 1.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AttributesSetup : MonoBehaviour
{
public int strMin;
public int myClass;
BaseSkills class1;
public void StrMin (int strMin) {
class1.Strength = 5;
print(class1.Strength); // I get 5
}
// Here I set the Strength value to 5.
public void Strength (int strengthLevel)
{
print(class1.Strength); // I get 1
}
Here I retrieve the value and get 1 and not 5, if I print after I set the value I get value 5.
I need the value set, so I can use the set value everywhere in any script. This is my first day in using getters and setters. I omitted some codes so its easier reading.
As much as I learned it, when I set class1.strength = str$$anonymous$$in, it should be set to 5 depending on character class value. But when I call it for later use, it went back to default. The $$anonymous$$ethod is attached to an new game button via GUI, and when you press the button, 5 gets called to str$$anonymous$$in, and different values to all other attributes. When pressing the arrow key, it should add Base Strength + Points added + Buffs. In this case just Strength + points.
Answer by d112570 · Jun 16, 2015 at 11:13 AM
The problem was solved at this post.
http://answers.unity3d.com/questions/987583/using-buttons-to-set-setters-not-working.html
Answer by DiegoSLTS · Jun 15, 2015 at 07:49 PM
Your StrMin method receives an strMin integer value that's never used, then changes the Strength property of class1 to 5 and prints it.
Your Strength method (in AttributesSetup) receives one strengthLevel value that's never used, and the prints class1.Strength value.
The missing info here is the code that calls StrMin and/or Strength, if you call Strength before StrMin, nothing else (at least in the code you showed) will change it's default value of 1.
Also, you are saying that the setter is not working but as you see when calling StrMin, the setter DOES work, if it wasn't working you wouldn't be getting a "5" printed.
Check again that you call Strength (the method of AttributesSetup) after you've set the Strength (the property of class1) to a different value. If it's still not working share some more code to find the real problem.
I did a little test, if I add the code inside the void awake it works.
void Awake ()
{
class1.Strength = 5;
}
public void Strength (int strengthLevel)
{
print(class1.Strength); // I get 5
}
if I move "class1.Strength = 5;" back inside the method it does not work.
public void Str$$anonymous$$in (int str$$anonymous$$in) {
class1.Strength = str$$anonymous$$in; // str$$anonymous$$in = 5
print(class1.Strength); // I get 5
}
public void Strength (int strengthLevel)
{
print(class1.Strength); // I get 1
}
Awake is called automatically by Unity when the scene is created. "Str$$anonymous$$in" is a method you have to call by yourself somewhere, just like "Strength".
Just having the methods in some script won't call them, it doesn't matter if you have Str$$anonymous$$in implemented above Strength, you have to call them.
Again, show us the code where you CALL Str$$anonymous$$in and Strenght, not the code where you implement those methods. $$anonymous$$ake sure Str$$anonymous$$in is called at least once before the call to Strength (if your code is hard to follow you can check that by adding prints at the begining of each method).
I added the full code below. When I press new game then str$$anonymous$$in = 5. And at the new game window I can edit the strengthLevel using the gui-buttons.
Here ist a screenshot of the button. AttributesSetup is also attached to the button.
Answer by Jessespike · Jun 15, 2015 at 07:51 PM
The get/set property looks OK. So it must be something else, from the code you're showing, it looks like the Strength function simply prints out class1's Strength, which by default would be 1.
public void Strength (int strengthLevel)
{
class1.Strength = strengthLevel; //add this to set the strength
print(class1.Strength);
}
need more codes, what's the order that you're calling these? Not enough info to be certain.
The full code is here. str$$anonymous$$in = 5
public void aStr$$anonymous$$in (int str$$anonymous$$in) {
str.text = str$$anonymous$$in.ToString();
GameObject.Find ("Strength").GetComponent<AttributesSetup> ().str$$anonymous$$in = str$$anonymous$$in;
class1.Strength = str$$anonymous$$in;
print(class1.Strength); // I get 5
}
public void Strength (int strengthLevel)
{
var pointsLeft = int.Parse (points.text);
if ((count+str$$anonymous$$in) >= str$$anonymous$$in && strengthLevel == 1 && pointsLeft >= 1) { // Add
count = count + strengthLevel;
pointsLeft--;
} else if ((count+str$$anonymous$$in) != str$$anonymous$$in && strengthLevel == -1) { // Subtract
count = count + strengthLevel;
pointsLeft++;
}
str.text = (count+str$$anonymous$$in).ToString();
points.text = pointsLeft.ToString();
class1.StrengthPoints = count;
var Strength = class1.StrengthPoints + class1.Strength;
print("Strength " + Strength + " - Points given " + class1.StrengthPoints);
print(class1.Strength); // I get 1
}
When I start new game, the void str$$anonymous$$in gets called. When adding extra points to strength void strength level gets called.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201025im_/https://answers.unity.com/themes/thub/images/avi.jpg)