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

Question Can't select ChildObject 0. Why?

Discussion in 'Scripting' started by SimpleDuude, Jun 13, 2023.

  1. SimpleDuude

    SimpleDuude

    Joined:
    May 22, 2021
    Posts:
    5
    Hello everyone, hope y'all have a good day. So here's my problem:
    I have a character with 6 slot inventory for weapons (0-5). Everything's fine, but the only issue is that, when 0 gets selected, it doesn't do anything. And 0 would represent the first weapon slot. The weapons, are childObjects of a weaponholder-Object. I tried out a few things but couldn't get the problem.
    I'm a beginner, maybe I'm doing something impossible or harder than it has to be.

    reloading Script:
    Code (CSharp):
    1.     IEnumerator Reload()
    2.     {
    3.         isReloading = true;
    4.  
    5.         Debug.Log("reloading...");
    6.  
    7.         yield return new WaitForSeconds(reloadTime);
    8.  
    9.         currentAmmo = maxAmmo;
    10.  
    11.         int randNumber = Random.Range(0, 5);
    12.         Debug.Log(randNumber);
    13.         weaponSwitching.selectorWeapon = randNumber;
    14.  
    15.         isReloading = false;
    16.     }

    Weaponchanging Script:
    Code (CSharp):
    1.     void selectedWeapon()
    2.     {
    3.         {
    4.             int i = 0;
    5.             foreach (Transform weapon in transform)
    6.             {
    7.                 Debug.Log("SelectorWeapon " + selectorWeapon);
    8.                 if (i == selectorWeapon)
    9.                 {
    10.                     weapon.gameObject.SetActive(true);
    11.                 }
    12.                 else
    13.                 {
    14.                     weapon.gameObject.SetActive(false);
    15.                 }
    16.  
    17.                 i++;
    18.             }
    19.         }
    20.     }
    21. }
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Maybe not your main problem, but Random.Range for ints excludes the last number, so you'll only ever get index 0 to 4 on line 11.
     
    Bunny83 and SimpleDuude like this.
  3. SimpleDuude

    SimpleDuude

    Joined:
    May 22, 2021
    Posts:
    5
    Not my main problem but you're right. I already noticed that the last isn't getting selected.
    Thanks!
     
    Bunny83 and mopthrow like this.
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,396
    Maybe debug the foreach and print all elements to see what it is enabling and disabling?
     
    Bunny83 likes this.
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Don't rely on sibling order. That's super-unspecified and unclear. Rely on something more tangible.

    For example, whatever populated the weapon children should govern their order. Other objects should have means of obtaining this list somehow.

    At least add a component to every child whose only job is to store the index. This saves you from having to pass the list between your objects, as each interested party can scan the children and assess the situation.

    You can also do a combination of these things.

    Regarding your problem, I can't see the problem in your selectedWeapon() method so it's not there.
    However you can re-write this kind of logic to a much cleaner
    Code (csharp):
    1. void selectedWeapon()
    2. {
    3.   int i = 0;
    4.   foreach (Transform weapon in transform)
    5.   {
    6.     Debug.Log("SelectorWeapon " + selectorWeapon);
    7.     weapon.gameObject.SetActive(i == selectorWeapon);
    8.     i++;
    9.   }
    10. }
    Also you have one extra set of block brackets. I rarely see those in projects.

    I won't criticize you for the hanging brackets, that's a stylistic choice for many and we don't argue taste. However, just consider how blank these lines look. Typically blank lines are used as visual separators, but with hanging brackets you're separating what should stay together.
     
    Last edited: Jun 13, 2023
    DevDunk likes this.
  6. SimpleDuude

    SimpleDuude

    Joined:
    May 22, 2021
    Posts:
    5
    Alright thanks!
    I just saw this approach in a youtube video and thought, that's maybe a good solution.
    I will try to fix it in a other way, maybe i will find a better way.

    As I said, I'm really new in developing, and I also don't have a style or something. I do not really watch on how the code looks, I just wanted to start first with setting up a game.
    But i will pay attention to it.

    Thank you very much!
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    This is easily one of the most abstract hard problems to do in Gamedev. It is NOT a great starting point whatsoever.

    These things (inventory, shop systems, character customization, dialog tree systems, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

    Inventory code never lives "all by itself." All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

    Inventories / shop systems / character selectors all contain elements of:

    - a database of items that you may possibly possess / equip
    - a database of the items that you actually possess / equip currently
    - perhaps another database of your "storage" area at home base?
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
    - interaction with items in the inventory or on the character or in the home base storage area
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of such a system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - if there is an item limit, what is it? Total count? Weight? Size? Something else?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items? How? Limits? Results? Messages of success/failure?
    - can users substantially modify items with other things like spells, gems, sockets, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    If you want to see most of the steps involved, make a "micro inventory" in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

    Everything you learn doing that "micro inventory" of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

    Breaking down large problems in general:

    https://forum.unity.com/threads/opt...n-an-asteroid-belt-game.1395319/#post-8781697
     
  8. SimpleDuude

    SimpleDuude

    Joined:
    May 22, 2021
    Posts:
    5
    I'm aware that this is a complex theme. Generally, the whole developement thing is complex.

    I already programmed some games that don't have a inventory or something in that way.
    I know the basics of walking, jumping, levels and so on and programmed games that I'm proud of that i got it.
    But the thing is that I need and want to have a "bigger" project. This project here, is clearly messy and hard.
    Maybe I will drop it after a while, but everything I learn here I can implement it in a other project in future.
    This is the test for me, to find out where my limits are.
    I'm a fan of roguelike games, like "Nuclear Throne, The binding of isaac....".
    I have notes and ideas what I wan't to create, I knew from the start that I need to make a plan.

    Thank you very much for the informations and help. I appreciate it very much.
    I'm happy you wrote that to me and explained everything and took your time.

    In this time, I even found a Solution for my problem:

    I really don't know why exactly 0 couldn't get selected, even with de debugs.
    But I created for every weapon slot a public gameObject.
    With this, I don't have to use the childObjects at all.
    In this way, every Slot will have a own gameObject and I can select it very easily.

    Code (CSharp):
    1.     void selectedWeapon()
    2.     {
    3.         int i = 0;
    4.         foreach (Transform weapon in transform)
    5.         {
    6.             Debug.Log("SelectWeapon " + selectorWeapon);
    7.             if (i == selectorWeapon -1)
    8.             {
    9.                 weapon.gameObject.SetActive(true);
    10.                 if (selectorWeapon == 1)
    11.                 {
    12.                     slot1.transform.gameObject.SetActive(true);
    13.                 }
    14.                 else if (selectorWeapon == 2)
    15.                 {
    16.                     slot2.transform.gameObject.SetActive(true);
    17.                 }
    18.                 else if (selectorWeapon == 3)
    19.                 {
    20.                     slot3.transform.gameObject.SetActive(true);
    21.                 }
    22.                 else if (selectorWeapon == 4)
    23.                 {
    24.                     slot4.transform.gameObject.SetActive(true);
    25.                 }
    26.                 else if (selectorWeapon == 5)
    27.                 {
    28.                     slot5.transform.gameObject.SetActive(true);
    29.                 }
    30.                 else if (selectorWeapon == 6)
    31.                 {
    32.                     slot6.transform.gameObject.SetActive(true);
    33.                 }
    34.             }
    35.             else
    36.             {
    37.                 weapon.gameObject.SetActive(false);
    38.             }
    39.             i++;
    40.         }
    41.     }
    Code (CSharp):
    1.     IEnumerator Reload()
    2.     {
    3.         isReloading = true;
    4.  
    5.         Debug.Log("reloading...");
    6.  
    7.         yield return new WaitForSeconds(reloadTime);
    8.  
    9.         currentAmmo = maxAmmo;
    10.  
    11.         int randNumber = Random.Range(1, 7);
    12.         Debug.Log(randNumber);
    13.         weaponSwitching.selectorWeapon = randNumber;
    14.  
    15.         isReloading = false;
    16.     }
     
    Kurt-Dekker likes this.