0
votes

To sum this up the best I can: I have some code that sets a boolean value to true. When this little checkbox is enabled, it works as intended, allowing the game to function normally. When set to true it does as expected

However, later on the code at some point sets this boolean to false (remember, it is a public boolean). Unfortunately, when this checkbox enabled. The console reads "true, false, true, false" etc. which prevents the desired outcome. You can see that the output is true, false, true, false when the checkbox is enabled

If I disable this checkbox, then the output reads "false, true, false, true" etc. which prevents anything from working. Nothing works!!!

Is there a way of disabling this stupid checkbox from having any effect in the game? I have tried [system.nonserialized] to no effect. At least, is there a way to disable and enable this checkbox in code?

As requested, here is the code:

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

public class startAgain : MonoBehaviour {
    public bool start;

    // Use this for initialization
    void Start () {
        //start = true;
    }

    public void use ()
    {
        start = true;
    }

    // Update is called once per frame
    void Update () {
        print(start);
    }

    public void rest()
    {
        StartCoroutine(Waittostart());
        Vector3 ball = GameObject.Find("Ball").transform.localPosition = new Vector3(0,0,0);
        Transform lpadlx = GameObject.Find("Player Paddle").transform;
        Vector3 lpadpos = lpadlx.position;
        Vector3 leftpaddle = GameObject.Find("Player Paddle").transform.position = new Vector3(lpadpos.x, 0, 0);

        Transform rpadlx = GameObject.Find("AI Paddle").transform;
        Vector3 rpadpos = rpadlx.position;
        Vector3 rightpaddle = GameObject.Find("AI Paddle").transform.position = new Vector3(rpadpos.x, 0, 0);
    }

    IEnumerator Waittostart()
    {
        print("RUNNING!");
        start = false;
        yield return new WaitForSeconds(2);
        start = true;
    }

}

(please note that some of this was used for debug, so some bits will be out of place just to test it!)

Thanks! I hope this was clear enough.

2
Where is the code for that class? - derHugo
Please check the code which I have added to the question! - Ollie R

2 Answers

1
votes

I tested the supplied code, and it worked as I would expect--the checkbox in the UI stayed in sync with the state of the bool in the running code. The console logs also consistently showed the correct value.

I was able to replicate the behavior you described by adding a second instance of the script. Try changing your print call to print($"{start} {GetHashCode()}");. You should see only one value for GetHashCode() in the Console. If you see two values, you definitely have two instances of the script.

0
votes

I fixed it! I removed the script from the game object it was attached to, and then set the boolean as static, and referenced it from there.

Thanks for the help anyway!