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

Easier way to do this?

Discussion in 'Scripting' started by Smotteh, Jun 25, 2016.

  1. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    So basically i have my code setup like this
    I know there is always another way, but i'm asking if someone can think of an easier way because i'm making like 30 different costumes and if there was a way to speed up the coding time that would be nice! Thanks! :D
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEngine.UI;
    6. public class customizationLogic : MonoBehaviour {
    7.    
    8.     public GameObject _ExampleHairPiece;
    9.     public GameObject _ExampleTorsoPiece;
    10.     public GameObject _ExampleLegPiece;
    11.     public GameObject _Example2HairPiece;
    12.     public GameObject _Example2TorsoPiece;
    13.     public GameObject _Example2LegPiece'
    14.    // Use this for initialization
    15.    void Start () {
    16.        _ExampleHairPiece.SetActive(false);
    17.        _ExampleTorsoPiece.SetActive(false);
    18.        _ExampleLegPiece.SetActive(false);
    19.        _Example2HairPiece.SetActive(false);
    20.        _Example2TorsoPiece.SetActive(false);
    21.        _Example2LegPiece.SetActive(false);
    22.  
    23.    }
    24.  
    25.    // Update is called once per frame
    26.    void Update () {
    27.      
    28.    }
    29.   public void _ExampleHairPieceSelect()
    30.    {
    31.        _ExamplePiece.SetActive(true);
    32.        _Example2HairPiece.SetActive(false);
    33.    }
    34.    public void _ExampleTorsoPieceSelect()
    35.    {
    36.        _ExampleTorsoPiece.SetActive(true);
    37.        _Example2TorsoPiece.SetActive(false);
    38.    }
    39.    public void _ExampleLegPieceSelect()
    40.    {
    41.        _ExampleLegPiece.SetActive(true);
    42.        _Example2LegPiece.SetActive(false);
    43.    }
    44.    public void _Example2HairPieceSelect()
    45.    {
    46.        _Example2HairPiece.SetActive(true);
    47.        _ExampleHairPiece.SetActive(false);
    48.    }
    49.    public void _Example2TorsoPieceSelect()
    50.    {
    51.        _Example2TorsoPiece.SetActive(true);
    52.        _ExampleTorsoPiece.SetActive(false);
    53.    }
    54.    public void _PunkRockLegPieceSelect()
    55.    {
    56.        _Example2LegPiece.SetActive(true);
    57.        _ExampleLegPiece.SetActive(false);
    58.    }
    59. }
    60.  
     
  2. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    Arrays are way to speed up coding and you should use them. Check them out!
     
    Smotteh likes this.
  3. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    Can you give me an example of an array? or maybe link me to a proper tutorial? Thanks :D
     
  4. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
  5. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    So if i make an array like
    Code (csharp):
    1.  
    2. public GameObject[] _hairPieces;
    3. public GameObject[] _torsoPieces;
    4. public GameObject [] _legPieces;
    5.  
    is there an easier way for me to do the selection (I.E _hairPiece1.setActive) etc..
     
  6. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    Code (CSharp):
    1.  
    2. _hairPieces[indexNumber].gameObject.SetActive();
    3.  
    4. //0 <= indexNumber < _hairPieces.Length;
    You can disable every other with array, swap elements, find the one you needby searching whole array...etc

    Edited to fix indexNumber range!
     
    Last edited: Jun 25, 2016
    Smotteh likes this.
  7. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Here is a super simple way to use arrays:
    Code (CSharp):
    1. // Populate this in inspector
    2. public GameObject[] pieces;
    3.  
    4. //Run this whenever needed
    5. foreach(GameObject g in pieces)
    6. {
    7.     g.SetActive(false);
    8.     // use g.SetActive(true) to enable each item in the array
    9. }
     
    Smotteh likes this.
  8. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    You need to make an <Item> script. where each item has a link to the body pieces. From the item condition you control if its on or not.

    Item can be a List type or array..
     
    Smotteh likes this.
  9. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    You could do something like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class CustomizationLogic : MonoBehaviour {
    6.  
    7.     // ========== VARS ==========
    8.  
    9.     public GameObject[] _allHairPieces;
    10.     public GameObject[] _allTorsoPieces;
    11.     public GameObject[] _allLegPieces;
    12.  
    13.     // Create costumes in the inspector. Index is the position they are at in the hair/torso/leg arrays.
    14.     [System.Serializable]
    15.     public class Costume
    16.     {
    17.         public int _hairIndex;
    18.         public int _torsoIndex;
    19.         public int _legIndex;
    20.     }
    21.  
    22.     public Costume[] _allCostumes;
    23.  
    24.     // ========== FUNCTIONS ==========
    25.  
    26.     void Start()
    27.     {
    28.         // Example: Use first costume in the _allCostumes array.
    29.         SwitchCostume(_allCostumes[0]);
    30.     }
    31.  
    32.     void SwitchCostume(Costume costume)
    33.     {
    34.         DisableAllCostumes();
    35.  
    36.         _allHairPieces[costume._hairIndex].SetActive(true);
    37.         _allTorsoPieces[costume._torsoIndex].SetActive(true);
    38.         _allLegPieces[costume._legIndex].SetActive(true);
    39.     }
    40.  
    41.     void DisableAllCostumes()
    42.     {
    43.         foreach(GameObject go in _allHairPieces)
    44.         {
    45.             go.SetActive(false);
    46.         }
    47.  
    48.         foreach(GameObject go in _allTorsoPieces)
    49.         {
    50.             go.SetActive(false);
    51.         }
    52.  
    53.         foreach(GameObject go in _allLegPieces)
    54.         {
    55.             go.SetActive(false);
    56.         }
    57.     }
    To use it do something like this:

    Code (csharp):
    1. SwitchCostume(_allCostumes[2]);
    Or if you had a constructor in the Costume class:

    Code (csharp):
    1. SwitchCostume(new Costume(1, 4, 3));
     
    Last edited: Jun 25, 2016
    Smotteh and TaleOf4Gamers like this.
  10. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    Thanks so much im going to try this out, i was dreading typing in each costume xD
     
  11. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    This is even simpler and faster and I'd say better :D ( I had nothing to do, sorry guys ;) )
    Code (CSharp):
    1.   using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     public class CustomizationLogic : MonoBehaviour {
    5.    
    6.      [System.Serializable]
    7.         public class Costume
    8.         {
    9.             public GameObject[] parts;
    10.         }
    11.    
    12.         public Costume[] _allCostumes;
    13.         private int previousCostumeIndex;
    14.    
    15.         void Start()
    16.         {
    17.             previousCostumeIndex = -1;
    18.             SwitchCostume(0); // Example: Use first costume in the _allCostumes array.
    19.         }
    20.    
    21.         void SwitchCostume(int ID)
    22.         {
    23.             ToggleCostume(previousCostumeIndex, false);
    24.             ToggleCostume(ID, true);
    25.             previousCostumeIndex = ID;
    26.         }
    27.    
    28.         void ToggleCostume(int ID, bool state)
    29.         {
    30.             if(ID < 0 || ID >= _allCostumes.Length) return;
    31.             Costume costume = _allCostumes[ID];
    32.             for(int i=0; i<costume.parts.Length; i++)
    33.                 costume.parts[i].SetActive(state);
    34.         }
    35.     }
     
    Smotteh likes this.
  12. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    I have everything set up with buttons so you could cross dress, i'm really sorry for the noob questions but how would i make it so i can cross dress them so for example torsoPieceOne, with legPiece2, and hairPiece3
     
  13. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    What OneDragutin posted was a manager where it will swap costumes. Simply drag that to a gameobject. When ever you want to swap things you access this script and it will enable that costume. Or you can make the manager a singletone where you can access it from another script. You need to know what ID to swap thought, thats why i recommend a item system where you can place icons so you know what item your asking for or swapping.
     
  14. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    Well what this is for is a character creation, i had it all working but the reason i didnt like my system (As posted above) is because everytime i add a new item to the character creation system i had to type everything out for it in the code. What do you mean by an item system? like with ids and stuff? (Sorry for all the noob questions guys)
     
  15. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Right, then a manager is what you need. You can customize just how you want it. A Manager is basically just controlling what is going to happen to your costumes, how and when it swap, and keeping track on what is going on.,

    I can show you quick example of item system. let me just finnhis my dinner :)
     
  16. Smotteh

    Smotteh

    Joined:
    May 28, 2015
    Posts:
    22
    ok ill pm you a screen shot of how i have it set up rn