• 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 Josh1231 · Jul 25, 2013 at 11:44 AM · c#networkingnetworkequip

C# How to make the gun remember who equipped it

I wanted to know how to make the gun remember who equipped it after it has been equipped. this is what i have so far:

 using UnityEngine;
 using System.Collections;
 
 public class GunStay : MonoBehaviour {
     
     public bool equip = false;
 
     // Use this for initialization
     void Start () {
     equip = false;
     }
     
     public void EquipGun () {
     if (equip == false)
         {
         equip = true;    
         }
     else
         {
         equip = false;    
         }
     }
     
     // Update is called once per frame
     void Update () {
     if (equip == true)
         {
         GameObject GunStay = GameObject.FindGameObjectWithTag("GunStay");
         if (GunStay.networkView.isMine)
             {
             Vector3 GunGo = GunStay.transform.position;
             Vector3 temp = transform.position;
             temp.x = GunGo.x;
             temp.y = GunGo.y;
             temp.z = GunGo.z;
             transform.position = temp;
             }
         }
     }
 }
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

1 Reply

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

Answer by amphoterik · Jul 25, 2013 at 11:55 AM

I would add a variable to contain the gun holder and modify the EquipGun method to be:

     GameObject currentHolder
     public void EquipGun (GameObject holder) {
     if (equip == false)
     {
         equip = true;
         currentHolder = holder;
     }
     else
     {
         equip = false;
         currentHolder = null;
     }
 }

Then in any script that can pick up a gun you would do:

 EquipGun(this);

You can find a child element like this:

 GameObject GunStay = currentHolder.transform.Find("GunStay");

In your code GunStay is a tag. I am using it like a name. Replace the name GunStay with whatever the object is actually named

Comment
Add comment · Show 7 · 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 Josh1231 · Jul 25, 2013 at 11:58 AM 0
Share

thank you, this helped lots

avatar image amphoterik · Jul 25, 2013 at 12:10 PM 0
Share

Glad I could help.

avatar image Josh1231 · Jul 25, 2013 at 12:45 PM 0
Share

how do i make sure it goes to this objects hands ins$$anonymous$$d of another since i don't know how to do a parent check with it

avatar image amphoterik · Jul 25, 2013 at 12:48 PM 0
Share

Is "GunStay" an object that represents the hands?

avatar image Josh1231 · Jul 25, 2013 at 12:51 PM 0
Share

yes it is.

Show more comments

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

16 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

Related Questions

C# How to have a gun select 1 Answer

How do I send a simple Vector3 over a network? 1 Answer

How can i send a Player list to every Client 0 Answers

modernized master server script? 1 Answer

Unity Multiplayer Android Game 0 Answers

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