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
2
Question by fireomega · Jan 23, 2012 at 10:18 PM · fpsweaponshooterpickupfirst

Weapon pick up

I've made a script which picks a weapon up after you touch it

  var Hand : Transform;
 var Sword : Transform;
 var customSkin : GUISkin;
 function OnTriggerEnter (other : Collider) {
    GetComponent("LockScript").enabled = false;
     if(other.gameObject.tag == "Sword"){
 GUI.skin = customSkin;
     GUI.Box (Rect (480,200,400,180), "Do you wish to pick this weapon up?");
 
         if (GUI.Button (Rect (500,270,100,60), "Yes")) {
         Sword.transform.position = Hand.position;
         Sword.transform.parent = Hand.transform;
         Destroy(other.gameObject);}
     
     if (GUI.Button (Rect (750,270,100,60), "No")) {
      GetComponent("LockSript").enabled = true;
     }
 }}

It looks as though it should work yet it doesn't. Any help would be appreciated

Comment
Add comment
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

3 Replies

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

Answer by aldonaletto · Jan 23, 2012 at 10:37 PM

You can't place GUI instructions anywhere you want, only inside the OnGUI function. You must set a boolean variable in OnTriggerEnter to enable the GUI elements inside OnGUI, and clear this variable when it's no more needed. You must also save a reference to the trigger object, so you can destroy it in OnGUI:

var Hand : Transform; var Sword : Transform; var customSkin : GUISkin; private var pickedObj : GameObject; private var enableGui = false;

function OnTriggerEnter (other : Collider) { GetComponent("LockScript").enabled = false; if(other.gameObject.tag == "Sword"){ enableGui = true; pickedObj = other.gameObject; } }

function OnGUI(){ if (enableGui){ GUI.skin = customSkin; GUI.Box (Rect (480,200,400,180), "Do you wish to pick this weapon up?"); if (GUI.Button (Rect (500,270,100,60), "Yes")) { // if YES pressed... Sword.transform.position = Hand.position; Sword.transform.parent = Hand.transform; Destroy(pickedObj); // destroy the picked object enableGui = false; // close GUI } if (GUI.Button (Rect (750,270,100,60), "No")) { // if NO pressed... GetComponent("LockSript").enabled = true; enableGUI = false; // close GUI } } }

Comment
Add comment · Show 2 · 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 fireomega · Jan 24, 2012 at 07:50 PM 1
Share

Thank you, worked perfectly much appreciated

avatar image doomprodigy · Jan 24, 2012 at 10:09 PM 2
Share

$$anonymous$$ay I note that you can and should accept the question if it is the one that solved your problem. So that others do not look at it, also to reduced the amount on unanswered questions.

avatar image
0

Answer by epicman245 · Mar 14, 2013 at 05:02 AM

Where do I put a script like that? On the gun I'm trying to pick up? Or on the player, or something else.

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
avatar image
0

Answer by Spy-King · Apr 30, 2015 at 07:51 AM

This script should be put on the gun prefab. It gives the player the weapon when you press the button Q.

(Btw, if your using a rigidbody to move the Player you should be fine. If you're using Raycasting, then you might want to add the a child to the player which has a boxcollider2D over the player and a rigidbody attached with no gravity)

Sorry, for the lengthy explanation, here's my script -

 using UnityEngine;
  
  public class GunPickup : MonoBehaviour
  {
      public SpriteRenderer PistolRenderer;
      public Player Player; //Player is the script which controls the player
  
      private SpriteRenderer _spriteRenderer;
  
      public float FireRate;
      public bool IsSingleBurst; //this is just a variable which tells if the gun is an automatic or a non-automatic
  
      public void Awake()
      {
          _spriteRenderer = GetComponent<SpriteRenderer>();
      }
  
      public void OnTriggerStay2D(Collider2D other)
      {
          if (other.gameObject.GetComponentInParent<Player>() != null && Input.GetKeyDown(KeyCode.Q))
          {
              PistolRenderer.sprite = _spriteRenderer.sprite; // assigns this gun to the player's pistol
              Player.FireRate = FireRate;
              Player.IsSingleBurst = IsSingleBurst;
              if (BurstWhenTaken != null)
                  Instantiate(BurstWhenTaken, transform.position, transform.rotation);
              Destroy(gameObject); // destroys this object
          }
      }
  }
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

8 People are following this question.

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

Related Questions

Can i make a first person shooter MMO with unity? 2 Answers

Any problem with using real weapons in a FPS game? 0 Answers

picking up guns 1 Answer

Error in code I cannot fix 1 Answer

picking up weapons script? 3 Answers


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