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
  4. Dismiss Notice

Loop cycle script

Discussion in 'Scripting' started by DragonFhangDev, Jan 31, 2021.

  1. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Hey guys, I have this loop cycle that finds all the info I put into my hierarchy. (Code below)

    What I'm trying to figure out is. What if I don't place anything into my items found area, how can I get it to say for example "No items dropped"

    Because not every battle area I do will have items so I'd rather it say something instead of a blank space.

    Thanks!


    Code (CSharp):
    1. public class BattleReward : MonoBehaviour
    2. {
    3.     public static BattleReward instance;
    4.  
    5.     public Text xpText, goldText, itemText;
    6.     public GameObject rewardScreen;
    7.  
    8.     public string[] rewardItems;
    9.     public int xpEarned;
    10.     public int goldEarned;
    11.  
    12.     public bool markQuestComplete;
    13.     public string questToMark;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         instance = this;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         /*if (Input.GetKeyDown(KeyCode.Y))
    25.         {
    26.             OpenRewardScreen(54, 100, new string[] { "Iron Sword", "Iron ChestPlate" });
    27.         }*/    
    28.     }
    29.  
    30.     public void OpenRewardScreen(int xp, int gold, string[] rewards)
    31.     {
    32.         AudioManager.instance.PlayMusic(6);
    33.         //Cursor.visible = true;    
    34.         xpEarned = xp;
    35.         goldEarned = gold;
    36.         rewardItems = rewards;
    37.    
    38.         xpText.text = "Everyone earned " + xpEarned + " xp!";
    39.         goldText.text = "Garrison earned " + goldEarned + " Gold!";
    40.         itemText.text = "";
    41.  
    42.         for (int i = 0; i < rewardItems.Length; i++)
    43.         {
    44.             itemText.text += rewards[i] + "\n";
    45.         }
    46.  
    47.         rewardScreen.SetActive(true);
    48.    
    49.     }
    50.  
    51.     public void CloseRewardScreen()
    52.     {
    53.         for (int i = 0; i < GameManager.instance.playerStats.Length; i++)
    54.         {
    55.             if (GameManager.instance.playerStats[i].gameObject.activeInHierarchy)
    56.             {
    57.                 GameManager.instance.playerStats[i].AddExp(xpEarned);            
    58.             }
    59.         }
    60.  
    61.         GameManager.instance.currentGold += goldEarned;
    62.  
    63.         for (int i = 0; i < rewardItems.Length; i++)
    64.         {
    65.             GameManager.instance.AddItem(rewardItems[i]);
    66.         }
    67.    
    68.         rewardScreen.SetActive(false);
    69.         GameManager.instance.battleActive = false;
    70.         PlayerController.instance.canTeleport = true;
    71.         //UIManager.instance.teleportPanel.SetActive(true);
    72.  
    73.         if (markQuestComplete)
    74.         {
    75.             QuestManager.instance.MarkQuestComplete(questToMark);
    76.         }
    77.  
    78.         //Cursor.visible = false;    
    79.         AudioManager.instance.PlayMusic(FindObjectOfType<CameraController>().musicToPlay);
    80.     }
    81. }
     
    Last edited: Jan 31, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Usually you just make a blank list, add items to it however you are finding them, and when you go to display you check the size of the list.

    If there are zero items in the list, display "Nothing!" or else say "You found X items!"

    I scanned your code above a few times and I still don't really see where you're scanning for anything so I can't really be more specific.
     
  3. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Thanks for the reply!

    The "display nothing or Didn't find anything dropped" is what I want to have shown instead of just a blank spot.. That's what I'm trying to figure out what to do I mean.

    If I put anything in there that's not an "Item" it throws an error in the console.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You start with an empty string.

    You iterate and fill it in.

    When you're done, if it's still empty, add "Nothing found" instead!

    ALSO: if you use the proper code tags your code above would have line numbers which would make it easy for me to say "on this line..." but I cannot do that.

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  5. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Ah my bad, I fixed the code. I'm kinda new and still learning all of the coding aspects here but it sounds easy enough to do. Thanks for the info.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Excellent! So right around line 46 you can ask if that field is still empty and put in "Nothing found"

    For a more robust setup, create a separate boolean such as FoundAnything, starting as false, then you can set it whenever anything is found and check it at the end. That's more robust because it doesn't rely on the description being a string. In the future you might want to use graphics to show what you found.