0

In my chess game, I have a scene of pieces choice - black or white. After the user clicks on one of the pawns he/she gets a popup message/ It looks like this:

enter image description here

On Ok button click, the scene changes to the one with the board:

enter image description here

When the user chose black pieces he/she should see them closer to him/her, while if the user chose white pieces, they should be at the front. By default, in my scene pieces which are closer are black. I tried to achieve this by adding a texture change script on each figure (they will differ for white and black pieces):

void Start () {
        GetComponent<Renderer>().material = Resources.Load<Material>)"Materials/Pieces/Marble/White Pawn");
    }

However, how can I disable this script when I redirect to the scene if the user chose black pieces and the default view is needed. Here is my code for popup window:

void OnGUI()
    {
        if (showPopUp)
        {
            GUI.Window(0, new Rect((Screen.width / 2) - 200, (Screen.height / 2) - 115
                   , 420, 180), ShowGUI, "Figures choice");

        }
    }

    void ShowGUI(int windowID)
    {
        RedirectToMenu redirect = new RedirectToMenu();
        guiStyle.fontSize = 22;
        guiStyle.normal.textColor = Color.white;
        GUI.Label(new Rect(80, 40, 200, 30), "You have chosen black pieces", guiStyle);

        if (GUI.Button(new Rect(180, 110, 75, 35), "OK")){
            showPopUp = false;
            redirect.LoadAsset("UpgradedRoom");
            SceneManager.LoadScene("UpgradedRoom");
        }
    }

I suppose I should access this script before loading the scene and disable if needed. But how can I access it outside of the scene with table, chessboard, and pieces? Or can I change the textures of game objects on another scene?

Cassie
  • 2,941
  • 8
  • 44
  • 92
  • 3
    First step is to remove all reference to `OnGUI` and `GUI.Label` and then re-write your game to use uGUI. See [this](https://unity3d.com/learn/tutorials/s/user-interface-ui) for new UI tutorial and [this](https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects/41392130?s=1|0.0000#41392130) for how to subscribe to event dynamically. – Programmer Dec 09 '17 at 13:43

1 Answers1

0

What I would do is use a static variable to remember whether the pieces are black or white. So, you'd set the variable before the scene loads and then check it after it is loaded. Therefore, if your class is called chess manager, your code for setting the variable might look like this:

public class ChessManager : Monobehavior {
    public enum ChessColor { Black, White }
    public static ChessColor playerColor

    public void OnGUI() {
        if(user chooses black) {
            playerColor = ChessColor.Black;
            //Load scene
        }
        else if(user chooses white) {
            playerColor = ChessColor.White;
            //Load scene
        }
    }
}

...And your code for enabling/disabling the script might be something like this, where ColorChanger is the script that sets the color:

public class ColorChanger : Monobehavior {
    public void Start() {
        if(ChessManager.playerColor == ChessManager.ChessColor.White) {
            //Set texture
        }
    }
}

This would set the texture to something else when the user chooses white once the new scene is loaded. Don't forget to replace the if statements in ChessManager with the code that executes when a chess piece is selected (I'm assuming you're using legacy buttons for that, so you should replace if(user chooses color) with if(GUI.Button)). Hope I could help!

Also, as previously noted by someone else in the comments, it would probably be best if you used UnityEngine.UI instead of Unity's outdated legacy GUI system, but depending upon the project it might not be worth the effort.

Douglas Dwyer
  • 562
  • 7
  • 14