How can i use checkboxes also in inspector and when game is running ?

public bool stateForward = false, stateReverse = false, stateRandom = false;

I want to make that in the Inspector if the game is running or not running when i check one of the checkboxes the two others will be unchecked. I mean that in any case only one checkbox can be checked each time.

1 Like

I have tried to do something like this and sounds stupid. Hahax. Sorry I am just a newbie.

void Start () {
        while (true)
        {
            if (stateForward == true)
            {
                stateRandom == false;
                stateReverse == false;
            }
            else if (stateReverse == true)
            {
                stateRandom == false;
                stateForward == false;
            }
            else if  (stateRandom == true)
            {
                stateForward == false;
                stateReverse == false;
            }
        }

but I have found this link. Maybe you can have a look meanwhile waiting for others pro-coders to reply. xD

https://forum.unity3d.com/threads/hiding-variables-in-inspector-depending-on-other-variables-value.119906/

https://docs.unity3d.com/ScriptReference/UI.ToggleGroup.html

Put [ExecuteInEditMode] before your class definition like this:

[ExecuteInEditMode]
public class PrintAwake : MonoBehaviour

Then use hero617 's script (I corrected some mistakes).
There is no better way to write this unless you use editor script or Reflect library.

void Start () {
        while (true)
        {
            if (stateForward == true)
            {
                stateRandom = false;
                stateReverse = false;
            }
            else if (stateReverse == true)
            {
                stateRandom = false;
                stateForward = false;
            }
            else if  (stateRandom == true)
            {
                stateForward = false;
                stateReverse = false;
            }
        }
1 Like

I have more code in the Start function and i’m getting unreached code detected…

void Start()
    {
        while (true)
        {
            if (stateForward == true)
            {
                stateRandom = false;
                stateReverse = false;
            }
            else if (stateReverse == true)
            {
                stateRandom = false;
                stateForward = false;
            }
            else if (stateRandom == true)
            {
                stateForward = false;
                stateReverse = false;
            }
        }
        anims = GetComponent<Animations>();
        waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
        objectsToMove = GameObject.FindGameObjectsWithTag("Robots");
        originalPosition = objectsToMove[0].transform.position;
    }

The anims Uncreachable code detected

while (true) is an ininite loop

Yeah, if you put a while(true) in your start, you’ll just get an infinite loop and your game will freeze. Not a good idea.

You’ll want to do this with editor scripting.

I just mindlessly copied function from above. Sorry about that.
Just put this code inside Update function instead of Start and remove while(true).
Edit:
Working script.

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

[ExecuteInEditMode]
public class CheckBoxes : MonoBehaviour {

    public bool stateForward = false, stateReverse = false, stateRandom = false;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        if (stateForward == true)
        {
            stateRandom = false;
            stateReverse = false;
        }
        else if (stateReverse == true)
        {
            stateRandom = false;
            stateForward = false;
        }
        else if (stateRandom == true)
        {
            stateForward = false;
            stateReverse = false;
        }
    }
}

I think the issue you’ll find with that code is that you won’t be able to go from certain checks to certain other checks. For example, if stateForward is checked, then you click on stateRandom, it will disable stateRandom in the first block of that if statement, and leave you with just stateForward.

I think trying to do this without editor scripting is a fool’s errand. It’s doable, but you’re gonna need like twice the amount of code (basically, you need to track which one was checked before in a private variable, and then make an OnValidate, and check EVERY COMBINATION of checkboxes + previous state) and if I’m not mistaken that will grow exponentially if you add more possible states.

OR

If you use editor scripting, you can have each checkbox represented by a property and an enum holding the main status:
(properties don’t show up in the default inspector unfortunately, but you can set them in a custom editor easily)

private enum State {None, Forward, Reverse, Random};
private State state;
public bool stateForward {
get {
return state == State.Forward;
}
set {
if (value) state = State.Forward;
else {
if (state == State.Forward) state = State.None;
}
}
}
//etc for the other two states

OR

just make it a public enum, which will show up as a dropdown in the inspector, no editor scripting needed.

public enum State {None, Forward, Reverse, Random};
public State state;