• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jcastaldo13 · Jun 11, 2014 at 04:24 PM · mecanim2d-platformerequip

How do I equip Weapons in 2d side scroller

I am trying to figure out how to equip weapons in my 2d side scroller game. I want to be able to click on an icon in my inventory and have that be my weapon. Right now I have the animation working but I cant figure out how to swap out the weapon for another. Should my weapons be sprites or prefabs. Below is my Inventory script. If you need any other info let me know. Im at a complete loss and couldnt find anything useful online the last week. Thanks

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Inventory : MonoBehaviour {
 
     public static Inventory inventory;
 
     private bool paused = false;
     public Rect position;
 
     public WeaponClassScript.WeaponCreaterClass currentWeapon = null;
 
 
     private Rect inventoryWindowRect = new Rect(300,100,410,410);
     
     //private Dictionary
     public Dictionary<int,Texture2D> inventoryNameDictionary = new Dictionary<int, Texture2D> ();
 
     void Awake()
     {    
         inventory = this;
 
         inventoryNameDictionary.Add (0, null);
         inventoryNameDictionary.Add (1, null);
         inventoryNameDictionary.Add (2, null);
         inventoryNameDictionary.Add (3, null);
         inventoryNameDictionary.Add (4, null);
         inventoryNameDictionary.Add (5, null);
         inventoryNameDictionary.Add (6, null);
         inventoryNameDictionary.Add (7, null);
         inventoryNameDictionary.Add (8, null);
         inventoryNameDictionary.Add (9, null);
         inventoryNameDictionary.Add (10, null);
         inventoryNameDictionary.Add (11, null);
         inventoryNameDictionary.Add (12, null);
         inventoryNameDictionary.Add (13, null);
         inventoryNameDictionary.Add (14, null);
         inventoryNameDictionary.Add (15, null);
         inventoryNameDictionary.Add (16, null);
         inventoryNameDictionary.Add (17, null);
         inventoryNameDictionary.Add (18, null);
         inventoryNameDictionary.Add (19, null);
         inventoryNameDictionary.Add (20, null);
 
     }
 
     ItemClassScript itemObject = new ItemClassScript();
     WeaponClassScript weaponObject = new WeaponClassScript();
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyUp(KeyCode.I))
         {
             paused = !paused;
         }
         
         if(paused)
             Time.timeScale = 0;
         else
             Time.timeScale = 1;
 
         Debug.Log(Inventory.inventory.currentWeapon);
 
     }
 
     void OnGUI()
     {
         if (paused)
 
             inventoryWindowRect = GUI.Window(0,inventoryWindowRect, inventoryWindowMethod,"Inventory");
     }
     
     public void inventoryWindowMethod(int windowId)
     {
         inventoryNameDictionary[0] = WeaponClassScript.weaponClass.woodenSwordIcon;
 
         GUILayout.BeginArea(new Rect(10,50,390,400));
 
         GUILayout.BeginHorizontal ();
         GUILayout.Label(ItemClassScript.itemClass.goldIcon);
         GUILayout.Label("" + Collector.collector.curGold );
         GUILayout.Label(ItemClassScript.itemClass.oreIcon);
         GUILayout.Label("" + Collector.collector.curOre);
         GUILayout.EndHorizontal ();
 
         GUILayout.BeginHorizontal ();
         GUILayout.Label("SWORDS");
         if(GUILayout.Button(inventoryNameDictionary[0], GUILayout.Height (50)))
             currentWeapon = WeaponClassScript.weaponClass.sword1; // This is where my problem is im sure.
         GUILayout.Button(inventoryNameDictionary[1], GUILayout.Height (50));
         GUILayout.Button(inventoryNameDictionary[2], GUILayout.Height (50));
         GUILayout.Button(inventoryNameDictionary[3], GUILayout.Height (50));
         GUILayout.Button(inventoryNameDictionary[4], GUILayout.Height (50));
         GUILayout.Button(inventoryNameDictionary[5], GUILayout.Height (50));
         GUILayout.EndHorizontal ();
         
         GUILayout.BeginHorizontal ();
         GUILayout.Label("SHIELDS");
         GUILayout.Button (inventoryNameDictionary[6], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[7], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[8], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[9], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[10], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[11], GUILayout.Height (50));
         GUILayout.EndHorizontal ();
         
         GUILayout.BeginHorizontal ();
         GUILayout.Label(" ARMOR");
         GUILayout.Button (inventoryNameDictionary[12], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[13], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[14], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[15], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[16], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[17], GUILayout.Height (50));
         GUILayout.EndHorizontal ();
 
         GUILayout.BeginHorizontal();
         GUILayout.Label("ITEMS");
         GUILayout.Button (inventoryNameDictionary[18], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[19], GUILayout.Height (50));
         GUILayout.Button (inventoryNameDictionary[20], GUILayout.Height (50));
         GUILayout.EndHorizontal();
         
         GUILayout.EndArea ();
     
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jcastaldo13 · Jun 11, 2014 at 04:32 PM 0
Share

Strange. I could have sworn I hit the code button. Sorry about that. Thank you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by 13dnizinski · Jun 11, 2014 at 05:19 PM

If you have a prefab of the object, here's what you do:

  1. Create a folder in the root of your project called "Resources".

  2. Put whatever prefab you want to 'equip' in the "Resources" folder.

  3. In code, you can use the instantiate function to instantiate it. The prefab is in the "Resources" folder so you can access it with the Resources.Load() function.

So if I had a "Resources" folder with a prefab named "pizza" inside it, I could make a new one assigned to the variable selector like this:

GameObject selector = Instantiate(Resources.Load("pizza")) as GameObject;

Whatever you want to instantiate goes in place of "pizza", so if you had subfolders in the "Resources" folder, you could do that too. So If you have a subfolder, "Foods" inside the "Resources" folder and the pizza prefab was in the "Foods" folder, you would do:

GameObject selector = Instantiate(Resources.Load("Foods/pizza")) as GameObject;

Hope this helps!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jcastaldo13 · Jun 11, 2014 at 06:52 PM 0
Share

Almost, It is instantiating the image which is great but it is just coming up at origin when I click on it in my inventory. Not to my gameobject I want it attached to.

avatar image jcastaldo13 · Jun 11, 2014 at 08:43 PM 0
Share

I want it attached as a child object

avatar image jcastaldo13 · Jun 11, 2014 at 09:05 PM 0
Share

Or just as the sprite for the gameobject its attached to

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a scene look darker in unity2d? 3 Answers

Best Way To Use Sprite For Platform Game 2 Answers

Quick Question. 2 Answers

How to switch between 2d characters? 0 Answers

I am having flickering issues with the ground 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges