Scripting Question: How can I create a "Hints" list?

Hello!

I know this is basic, but I’m a little lost.

I have a Left Button and a Right Button, when the player presses the Right Button the default tip becomes disabled and the next tip becomes enabled. There are 12 tips.

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

public class Tips : MonoBehaviour
{
    public GameObject tipsManager;
    public GameObject tips;
    public Text tip1;
    public Text tip2;
    public Text tip3; //...etc

    private void Start()
    {
        tipsManager.SetActive(true);
    }
    public void DisableTips() //Hitting the X button disables the Tips Manager.
    {
        tipsManager.SetActive(false);
    }
    public void NextTip() //When the > button is presssed.
    {

    }
    public void PreviousTip() //When the < button is pressed.
    {

    }
}

I think I need to use a List? I’m not sure… I would really appreciate some direction.

Thank you!

You should be using an array or a list, yes. If you ever find yourself “numbering” your variables like this, an array or list would be a better option.

1 Like

FYI: Please use code-tags (not images) when posting code so devs can refer to your code correctly.

2 Likes

Oh, ok! Whats the best way to do that in this case? I’m sorry, it appears I have missed some fundamentals… I took a couple weeks off and I forget what I’m doing. LOL

Yes – put the hints into a List or an Array and use an integer to know which one you’re on. There are lots of places to look for this – faster than waiting for someone to retype them here. “Unity make list in Inspector” “C# list of strings”, “C# how to print item in list” and so on. It’s pretty generic programming. Once you have the hint in a string, “Unity display string” would help.

Nice! That solves the List issue, I’m sorry but I forget how to cycle through these. I think this would be an ++ for Next Tip and a – for Previous tip. Will need to SetActive the previous tip to false and SetActive the next tip to true, but… Couldn’t find any good results in google for this particular function.

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

public class Tips : MonoBehaviour
{
    public GameObject tipsManager;
    public List<GameObject> tips = new List<GameObject>();

    private void Start()
    {
        tipsManager.SetActive(true);
    }
    public void DisableTips() //Hitting the X button disables the Tips Manager.
    {
        tipsManager.SetActive(false);
    }
    public void NextTip() //When the > button is presssed.
    {
     
    }
    public void PreviousTip() //When the < button is pressed.
    {

    }
}

You would need a variable like int currentTipIndex and yes increment it to go to the next tip, decrement to go to the previous. Don’t forget to handle the edge cases when you’re at the beginning/end of the list.

I appreciate the help folks but I could really use an example. Please and thank you.

Of what? Incrementing??

currentTipIndex++;

Comparing and wrapping?

if (currentTipIndex >= MaxCount)
{
  currentTipIndex = 0;
}

Something else?

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)
2 Likes

Yes, incrementing to SetActive(true) the next List item and SetActive(false) the previous. How should I write the NextTip() method?

NextTip() should just be something like:

currentTipIndex++;
if (currentTipIndex >= tips.Count)
{
  currentTipIndex = 0;
}

To select and enable only that one thing out of a list, I like this pattern:

for (int tip = 0; tip < tips.Count; tip++)
{
  bool active = (currentTipIndex == tip);  // is it the one we want?
  tips[ tip].SetActive( active);
}
1 Like

It works perfectly! Thank you, Mr. Dekker, I will study this profusely!

2 Likes

This means a lot to me, thank you everyone for your help, especially Kurt!

2 Likes