Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Joey · Mar 31, 2011 at 03:18 AM · inventoryswitchweaponspropsswitch objects

Help needed in changing weapons

hello everyone

i am in the process of making a weapon switching/looting script

here is how my system works: player interacts with the item of the floor.

the script gets a message that the player has the item = true. The GUI button for the item is drawn. If the button is pressed then the item is shown as your current equipped item.

using UnityEngine;

using System.Collections;

public class MainGUI : MonoBehaviour

{

public bool _displayinventory = true;

public bool _displaycharacterwindow;

public GUISkin myskin;

public GameObject curwep;

public GameObject PhotonCanon;

public GameObject RxBlaster;

public static bool _hasPhotonCanon = false;

public static bool _hasRxblaster = false;

// Update is called once per frame

void Update ()

{

}

void OnGUI ()

{

 GUI.skin = myskin;



 if (_hasPhotonCanon) {

     //Debug.Log ("you have obtained a photon canon");



     if (GUI.Button (new Rect (460, 750, 90, 70), "", "photoncanon")) {

         curwep = PhotonCanon;

         Debug.Log ("i iwll equip to you" + curwep.ToString ());



     }



 }



 if (_hasRxblaster) {

     //Debug.Log ("you have obtained a Rx-Blaster");



     if (GUI.Button (new Rect (570, 750, 90, 70), "", "Rx-blaster")) {

         Debug.Log ("i wll equip to you RX ON !!!!" + curwep.ToString ());

         curwep = RxBlaster;

     }

     }

     }


 }

Here is the problem. first of all i am trying to make it so that the previous item that i had equipped turns off ( the gameobject turns off) and then the new item that has now been assigned to be the current weapon (curwep) turns on (the gamobject turns on).

I have attached my code. Can someone please take a look at this and please tell me how i should approach what i am trying to do

by the way this is all written in c# for those of you who haven't noticed.

Thanks in advance

Regards

Comment
Add comment · Show 2
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 Proclyon · Mar 31, 2011 at 07:35 AM 0
Share

Your code format didn't work out ( see it all the time , it's some selection glitch) Could you please fix that so all code is in the format block? Tx in advance!

avatar image Joey · Mar 31, 2011 at 11:20 PM 0
Share

can anyone actually help me please

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Robert Attard · Jul 16, 2011 at 01:24 AM

You can create the weapons as prefabs and attach them to a character bone. Attach this script to your game character

 public class Player : MonoBehaviour
  {
 //attach 2 weapon prefabs to weapon1 and weapon2
 //attach a bone Transform (head, foot, hand etc..) from your character to bone;
 
     public Transform weapon1;
     public Transform weapon2;
     public Transform bone;
     private Transform currentweapon;
 
     void Start()
     {
         //we attach weapon1 first;
         currentweapon = Instantiate(weapon1, bone.position, bone.rotation) as Transform;
         currentweapon.parent = bone;
     }
 
     public void removeCurrentWeapon()
     {
         currentweapon.parent = null;
         Destroy(currentweapon.transform.gameObject);
     }
 
    void Update()
    {
    //press the right control button will swap between weapon1 and weapon2
    // this is  just for testing purposes, you will obviously have to do something much more  
    // sophisticated
             if (currentweapon != null)
             {
                 if (currentweapon.name == "weapon1(Clone)")
                 {
                     removeCurrentWeapon();
                     currentweapon = Instantiate(weapon2, bone.position, bone.rotation) as Transform;
                     currentweapon.parent = bone;
                 }
                 else
                 {
                     removeCurrentWeapon();
                     currentweapon = Instantiate(weapon1, bone.position, bone.rotation) as Transform;
                     currentweapon.parent = bone;
                 }
             }
      }
 }

I fully tested it so it should work..

Comment
Add comment · Show 1 · 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 Robert Attard · Jul 16, 2011 at 01:45 AM 0
Share

I also done a quick video to show you the above code in action http://www.youtube.com/watch?v=VH0ZfEv_ouc

avatar image
0

Answer by Ermarrero · Apr 05, 2014 at 04:30 AM

Try this in update () .. cheers.

 if (_hasPhotonCanon == true) _hasRxblaster = false;
 else _hasPhotonCanon = false;
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

What's wrong with my weapon switching/swapping script? 0 Answers

FPS Weapon inventory script help 0 Answers

Issues with switching cameras 1 Answer

Switching/Picking up items in a third-person game 2 Answers

Creating an inventory/weapon array and ability to scroll through it 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges