0

Minimal code from BoardScript.cs

public class BoardScript : MonoBehaviour {
    private float angle;
    private static BoardScript instance;

    void Awake() {
        if (instance == null) {
            instance = this;
        }
        else if (instance != this) {
            Destroy(gameObject);
        }
     }

    Start(){
        angle = 0.5f;
    }

    Update(){
        Debug.Log("angle = " + angle); // logged angle never changes.
        Debug.Log("GetAngle() returns " + GetAngle()); // also never changes...
     }

    public SetAngle(float arg) {
        angle = arg;
        Debug.Log("angle set to " + angle);
        // Above always logs new angle as called fron Settings.cs. 
    }

    private float GetAngle() { // only use is for debugging this issue.
        Debug.Log("angle is " + angle); // Does not log newly set angle.
        return angle;
    }
}

Minimal code Settings.cs

private GameObject theBoard;

public void SetBoardAngule(float arg) {
    theBoard.GetComponent<BoardScript>().SetAngle(arg);
    Debug.Log("Settings - SetAngle to " + arg); // Logs correct angle.
}

The update function logs "angle = 0.5" no matter what it gets set to in method SetAngle(). This seems terribly simple to me. What am I doing incorrectly?

7
  • Are you sure, that you are calling GetAngle on the same instance, that you are calling SetAngle on? You should share the code, where you call SetAngle, too Commented Apr 13, 2019 at 0:00
  • I think so - see expanded code sample. Commented Apr 13, 2019 at 0:13
  • Instead of theBoard.GetComponent<BoardScript>().SetAngle(arg); try BoardScript.instance.SetAngle(arg) Commented Apr 13, 2019 at 0:20
  • 1
    @RigidBody won't compile because your instance is private. The singleton should be public for access though. Commented Apr 13, 2019 at 0:51
  • 1
    And always call BoardScript.instance.function Commented Apr 13, 2019 at 0:55

2 Answers 2

1

First thing first: your getter setter is all good.

other stuff: first thing if you are coming from Java world, you do not need to create functions for getter setters:

public SetAngle(float arg) {
        angle = arg;
        Debug.Log("angle set to " + angle);
        // Above always logs new angle as called fron Settings.cs. 
    }

    private float GetAngle() { // only use is for debugging this issue.
        Debug.Log("angle is " + angle); // Does not log newly set angle.
        return angle;
    }

can be converted to:

public float Angle
{
    get{return _angle;}
    set{_angle = value;}
}

second, i'm not sure why you do destroy the object, without creating one on awake: but here what you do is, if object doesn't exist, create one, .... otherwise if object exist, destroy current, but not creating another one.

third: which also relate to second: we are creating singleton, to have only one instance of our class, if that's what you want, there are tons of way to do it, but you have to set your variable accordingly: currently you are setting the value on (this instance) which may change from the (stored instance), and once that happen, you loss pointers, and you have to call it on the singleton class:

public float Angle
{
    get{return BoardScript.instance._angle;}
    set{BoardScript.instance._angle = value;}
}

instead of:

public float Angle
{
    get{return this._angle;}
    set{this._angle = value;}
}

forth: which again is related to second: at first your object instance will be stored inside instance variable, but if you creating another object which call awake (i'm forgotten how awake worked and got invoked) that object will cause current object to get disposed, i'm not sure if this is the behaviour that you want, just saying...

void Awake() {
    if (instance == null) {
        instance = this;
    }
    else if (instance != this) {
        Destroy(gameObject);
    }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

@RigidBody it's nice that i could be useful after so long
0

As pointed out by commentators, change

private static BoardScript instance;

to

public BoardScript instance;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.