Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Objects in a toolbox

Discussion in 'Scripting' started by Twidusk Studios, Feb 1, 2011.

  1. Twidusk Studios

    Twidusk Studios

    Joined:
    Aug 13, 2010
    Posts:
    42
    :) Hey guys. In my game, when you press Q, a toolbox opens, and i want to populate it with items. i am starting out with a simple block, defined by a prefab. anyways, i want to make a struct or class (Toolbox_Item) and be able to specify an icon and a 3D model . It would look like this:

    Code (csharp):
    1. var BasicBlock : Toolbox_Item = Toolbox_Item(Texture2D, Transform)
    is this possible? and if it is, how do I do this? i hope for a good answer that explains this in depth :-|
     
    Last edited: Feb 1, 2011
  2. Twidusk Studios

    Twidusk Studios

    Joined:
    Aug 13, 2010
    Posts:
    42
    This is in JavaScript.
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Generally, new game objects are created using the Instantiate() function. Is that what you're looking for?
     
  4. Twidusk Studios

    Twidusk Studios

    Joined:
    Aug 13, 2010
    Posts:
    42
    Nope, I want to show the object in my toolbox as a button with an icon
    and then when its clicked instantiate it
    but i know how to do that. HOW DO I MAKE A CLASS OR OBJECT WHERE
    I CAN DEFINE THE ICON AND MODEL???
     
  5. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    So many different ways you could implement it. Mainly comes down to whether you want to drag/drop icons/prefabs in the inspector or do it all in code.

    simple inspector version -
    Code (csharp):
    1.  
    2. // space for 10 prefabs/icons in inspector
    3. public GameObject[] toolboxPrefabs = new GameObject[10];
    4. public Texture2D[] toolboxIcons = new Texture2D[10];
    5.  
    more complex script version -
    Code (csharp):
    1.  
    2. public class Toolbox_Item
    3. {
    4.   public string prefabName; // prefab to Resource.Load();
    5.   public GameObject prefabRef; // object prefab created
    6.   public Texture2D iconTexture; // icon
    7. }
    8.  
    9. List<Toolbox_Item> toolbox = new List<Toolbox_Item>;
    10.  
    11.