Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Chocolade, Apr 6, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Code (csharp):
    1.  
    2. public bool stateForward = false, stateReverse = false, stateRandom = false;
    3.  
    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.
     
    hero617 likes this.
  2. hero617

    hero617

    Joined:
    Apr 6, 2017
    Posts:
    7
    I have tried to do something like this and sounds stupid. Hahax. Sorry I am just a newbie.
    Code (CSharp):
    1. void Start () {
    2.         while (true)
    3.         {
    4.             if (stateForward == true)
    5.             {
    6.                 stateRandom == false;
    7.                 stateReverse == false;
    8.             }
    9.             else if (stateReverse == true)
    10.             {
    11.                 stateRandom == false;
    12.                 stateForward == false;
    13.             }
    14.             else if  (stateRandom == true)
    15.             {
    16.                 stateForward == false;
    17.                 stateReverse == false;
    18.             }
    19.         }
    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/h...or-depending-on-other-variables-value.119906/
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
  4. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    Put [ExecuteInEditMode] before your class definition like this:
    Code (CSharp):
    1. [ExecuteInEditMode]
    2. 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.
    Code (CSharp):
    1. void Start () {
    2.         while (true)
    3.         {
    4.             if (stateForward == true)
    5.             {
    6.                 stateRandom = false;
    7.                 stateReverse = false;
    8.             }
    9.             else if (stateReverse == true)
    10.             {
    11.                 stateRandom = false;
    12.                 stateForward = false;
    13.             }
    14.             else if  (stateRandom == true)
    15.             {
    16.                 stateForward = false;
    17.                 stateReverse = false;
    18.             }
    19.         }
     
    hero617 likes this.
  5. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916

    I have more code in the Start function and i'm getting unreached code detected...

    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         while (true)
    5.         {
    6.             if (stateForward == true)
    7.             {
    8.                 stateRandom = false;
    9.                 stateReverse = false;
    10.             }
    11.             else if (stateReverse == true)
    12.             {
    13.                 stateRandom = false;
    14.                 stateForward = false;
    15.             }
    16.             else if (stateRandom == true)
    17.             {
    18.                 stateForward = false;
    19.                 stateReverse = false;
    20.             }
    21.         }
    22.         anims = GetComponent<Animations>();
    23.         waypoints = GameObject.FindGameObjectsWithTag("ClonedObject");
    24.         objectsToMove = GameObject.FindGameObjectsWithTag("Robots");
    25.         originalPosition = objectsToMove[0].transform.position;
    26.     }
    27.  
    The anims Uncreachable code detected
     
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    while (true) is an ininite loop
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    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.
     
  8. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. public class CheckBoxes : MonoBehaviour {
    7.  
    8.     public bool stateForward = false, stateReverse = false, stateRandom = false;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.        
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.         if (stateForward == true)
    18.         {
    19.             stateRandom = false;
    20.             stateReverse = false;
    21.         }
    22.         else if (stateReverse == true)
    23.         {
    24.             stateRandom = false;
    25.             stateForward = false;
    26.         }
    27.         else if (stateRandom == true)
    28.         {
    29.             stateForward = false;
    30.             stateReverse = false;
    31.         }
    32.     }
    33. }
     
    Last edited: Apr 7, 2017
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    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)
    Code (csharp):
    1. private enum State {None, Forward, Reverse, Random};
    2. private State state;
    3. public bool stateForward {
    4. get {
    5. return state == State.Forward;
    6. }
    7. set {
    8. if (value) state = State.Forward;
    9. else {
    10. if (state == State.Forward) state = State.None;
    11. }
    12. }
    13. }
    14. //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.
    Code (csharp):
    1. public enum State {None, Forward, Reverse, Random};
    2. public State state;