Search Unity

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

Discussion in 'Scripting' started by rbmethod, Feb 16, 2024.

  1. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Tips : MonoBehaviour
    7. {
    8.     public GameObject tipsManager;
    9.     public GameObject tips;
    10.     public Text tip1;
    11.     public Text tip2;
    12.     public Text tip3; //...etc
    13.  
    14.     private void Start()
    15.     {
    16.         tipsManager.SetActive(true);
    17.     }
    18.     public void DisableTips() //Hitting the X button disables the Tips Manager.
    19.     {
    20.         tipsManager.SetActive(false);
    21.     }
    22.     public void NextTip() //When the > button is presssed.
    23.     {
    24.  
    25.     }
    26.     public void PreviousTip() //When the < button is pressed.
    27.     {
    28.  
    29.     }
    30. }
    I think I need to use a List? I'm not sure... I would really appreciate some direction.

    Thank you!
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,907
    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.
     
    rbmethod likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,409
    FYI: Please use code-tags (not images) when posting code so devs can refer to your code correctly.
     
    rbmethod and PraetorBlue like this.
  4. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    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
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,996
    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.
     
  6. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Tips : MonoBehaviour
    7. {
    8.     public GameObject tipsManager;
    9.     public List<GameObject> tips = new List<GameObject>();
    10.  
    11.     private void Start()
    12.     {
    13.         tipsManager.SetActive(true);
    14.     }
    15.     public void DisableTips() //Hitting the X button disables the Tips Manager.
    16.     {
    17.         tipsManager.SetActive(false);
    18.     }
    19.     public void NextTip() //When the > button is presssed.
    20.     {
    21.      
    22.     }
    23.     public void PreviousTip() //When the < button is pressed.
    24.     {
    25.  
    26.     }
    27. }
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,907
    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.
     
  8. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    I appreciate the help folks but I could really use an example. Please and thank you.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,656
    Of what? Incrementing??

    Code (csharp):
    1. currentTipIndex++;
    Comparing and wrapping?

    Code (csharp):
    1. if (currentTipIndex >= MaxCount)
    2. {
    3.   currentTipIndex = 0;
    4. }
    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!!!)
     
    Nad_B and PraetorBlue like this.
  10. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    Yes, incrementing to SetActive(true) the next List item and SetActive(false) the previous. How should I write the NextTip() method?
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,656
    NextTip() should just be something like:

    Code (csharp):
    1. currentTipIndex++;
    2. if (currentTipIndex >= tips.Count)
    3. {
    4.   currentTipIndex = 0;
    5. }
    To select and enable only that one thing out of a list, I like this pattern:

    Code (csharp):
    1. for (int tip = 0; tip < tips.Count; tip++)
    2. {
    3.   bool active = (currentTipIndex == tip);  // is it the one we want?
    4.   tips[ tip].SetActive( active);
    5. }
     
    Nad_B likes this.
  12. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    It works perfectly! Thank you, Mr. Dekker, I will study this profusely!
     
    Kurt-Dekker and Nad_B like this.
  13. rbmethod

    rbmethod

    Joined:
    Oct 10, 2012
    Posts:
    100
    This means a lot to me, thank you everyone for your help, especially Kurt!
     
    Kurt-Dekker and Nad_B like this.