Working on buttons – same content across levels

So a problem I’ve been having – that the below returns objects in a random order:

GameObject[] buttons = GameObject.FindGameObjectsWithTag(Button);

        foreach (GameObject button in buttons)
        

is apparently a known thing, and can’t be controlled (as discussed in this thread).  So that means  if I want it left to right, I need to retrieve the children of the Canvas, instead of getting objects tagged as “Button”.  Grrr.

I don’t think I actually need to do this, after all, because what I’m actually going to do is instantiate each of these buttons from prefabs. Easy peasy.

ARE YOU KIDDING ME, PREFABS DON’T SAVE THE MANUAL “ONCLICK” THING INSIDE BUTTONS?  ARHGGHGH WHAT IS EVEN THE POINT OF THAT THEN WHY NOT JUST SCRIPT EVERYTHING.

Grrrr.  Oh.  I see.  It loses that connection because it was inside the Editor that it was connected to the Main Camera’s script.  So.. do I need a prefab of the Main Camera or something?   Cool.  Did that, updated the prefabs from the buttons to connect to the Main Camera prefab (this feels a little cheaty slash convoluted but oh well?), and now things work great.

       foreach (string level in levels) // yield sign says level isnt used ...? 
        {

        // everything down here is totally redundant but it works so oh well 
            if (xMult == 0)  
            {
                yMult = yMult1;
                xMult = 3;
                xSpace = 0; 

                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
                GameObject button = Instantiate(buttonLearn, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                xMult += 1;
                xSpace += 160; 
            }
            else if (xMult == 2)
            {
                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
                GameObject button = Instantiate(buttonPractice, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                xMult += 1;
                xSpace += 160; 
            }

            else
            {
                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult, 10f);
                GameObject button = Instantiate(buttonShowOff, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                xMult += 1;
                xSpace += 160; 
            }

So now it’s making buttons in the right color, prefab, order, etc.  Now I need it:

  • to look prettier / be better spaced
  • not change levels until the row is done  – that’s something about the name script
  • be well-labelled for kiddos (clear about how many points each button is)
  • have levels that make more sense for learning early concepts

Alright I made it so it doesn’t change levels till row is done, but now things are SERIOUSLY buggy.  It’s not resetting because of something with the Notebook/Trash entries.  Doesn’t seem to have anything to do with the code I put into NameButtons():

            buttonSpacer += 1;
            if (buttonSpacer == 3)
            {
                buttonCounter += 1;
                buttonSpacer = 0;
            }

As always, that particular problem is caused by it not reading in the .txt correctly.  It’s something about converting the string to an integer.  Have no idea what it is about the Unity Editor that messes with that, but … oh well.  (#todo – continue google searching for why this is happening.  Someday I’ll find the answer!)  Fixed it by changing what .txt file it was reading in.  Le sigh.

This one restructures it so that there’s space between the rows and the columns.

  void CreateButtons()
    {

        GameObject canvas = GameObject.Find(Canvas);

        // pull in info 
        TextAsset txtFile = (TextAsset)Resources.Load(PuzzleBotGoogleLevel, typeof(TextAsset)); // redundant :(  lots of scripts do this ... condense? put it in GM? 
        string[] levels = txtFile.text.Split(\n); // #todo why does \r not work? 

        // space buttons out on screen 
        float bufferX = Screen.width  50f;
        float bufferY = Screen.height  100f;
        float baseX = (bufferX / 3f); 
        float baseY = bufferY / 3f; 

        int xMult = 0; // set it to 0, if loop will convert to 3  
        int yMult = 4; 
        int xSpace = 0;
        int ySpace = 150 ;
       
        int buttonCounter = 0;

        foreach (string level in levels) // yield sign says level isnt used ...? 
        {

        // everything down here is totally redundant but it works so oh well 
            if (xMult == 0)  
            {
                yMult = yMult1;
                xMult = 3;
                xSpace = 0; 

                xSpace += 160; 
                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult + ySpace, 10f);
                GameObject button = Instantiate(buttonLearn, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                xMult += 1;

            }
            else if (xMult == 2)
            {
                xSpace += 160;

                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult + ySpace, 10f);
                GameObject button = Instantiate(buttonPractice, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                xMult += 1;
                 
            }

            else
            {
                xSpace += 160; 

                Vector3 buttonPosition = new Vector3(baseX * xMult + xSpace, baseY * yMult + ySpace, 10f);
                GameObject button = Instantiate(buttonShowOff, buttonPosition, Quaternion.identity) as GameObject;
                button.transform.SetParent(canvas.transform, false);    
                buttonCounter += 1;
                xMult += 1;
                ySpace -= 50; 
            }

        } 

    }

Let’s go back to my list above:

So now it’s making buttons in the right color, prefab, order, etc.  Now I need it:

  • to look prettier / be better spaced 
  • not change levels until the row is done  – that’s something about the name script 
  • be well-labelled for kiddos (clear about how many points each button is)
  • have levels that make more sense for learning early concepts

And I’m going to add:

  • label buttons appropriately (this is going to do a weird thing where it’s going to make it hard for me to call up the level, since I’ve been using button numbers to do that.  How about I keep the level number in there, to just keep things simple for now.)
    • This now just says level:  buttonText.text = levels[buttonCounter].Split(\t)[0];
    • This one says level and word family :  buttonText.text = levels[buttonCounter].Split(\t)[0] + \t + levels[buttonCounter].Split(\t)[2];    // switched to this so it just says level, not content
    • #todo – in playtesting, figure out what kids like in these buttons – how much info to give?

Just changed all the button prefabs to be 80 wide (kept 30 height).  Looks MUCH better.  I thought I had to change size through scale, but was wrong (yayy cuz scale affects text in unpleasant ways).

Okay so now I need to be true to my word and actually have scoreValue change inside each type of button. [#todo – playtest if it’s better to shower them with points, or if it’s better to be medium, or to have it be relatively conservative?]

I define a public int inside the GM that’s different for each type of Scene (which I already have if{} loops for so it defines where the audio happens).

    public void Audio()
    {
        Scene scene = SceneManager.GetActiveScene();
        if (scene.name == 1Learn)
        {
            audioList.Add(Onset);
            audioList.Add(Ending);
            audioList.Add(Answer);

            scoreValue = 5; 
        }
        else if (scene.name == 2Practice)
        {
            audioList.Clear();
            audioList.Add(Ending);

            scoreValue = 10;
        }
        else if (scene.name == 3ShowOff)
        {
            audioList.Clear();

            scoreValue = 15; 
        }

In PlayerFinalWord.cs, I changed it from defining scoreValue inside that loop to it pulling it in from the GM instance.

GameManager.instance.AddScore(GameManager.instance.scoreValue); 

Huh.  Just realized it’s only making 2 rows of buttons (6 total), where it the txt file actually has 9 rows (27 total buttons).  Why does it stop after the second row?   I only really want 3 rows, but it should be able to do 9.   Therrrrre we go, CreateButtons() was referring to the wrong .txt file.  Yay that was easier than I thought it would be!

#todo – draw a map of everything in the project, how scripts connect, etc.  Do some “official” documentation (that is, outside of this blog).

 

Leave a Reply

Your email address will not be published. Required fields are marked *