Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Madmanmpu · Jul 16, 2018 at 07:15 AM · networkingcollidertriggerdestroy objectspawning problems

Multiplayer: I have an OnTrigger script that isn't correctly responding. Spawning and Destroying GameObjects.

Hello! This is my first Networking game on Unity, and I'm having issues with spawning/destroying items. In short, this is what my script is trying to do. The script is attached to a Game Object with a Box Collider. Is Trigger checked on the Box Collider. When a player enters the trigger, text is enabled and the player is essentially asked to pick up the weapon. OnTriggerStay, if the player presses the "F" key (Action) then the prefab on the ground gets destroyed, and another prefab spawns to the players equipZone (hands). If the player is already holding a weapon, they can either "swap" weapons, or pickup the ammo from the weapon location. OnTriggerExit, the display text is disabled, and that's pretty much it.

As the code sits, I made 3 of the functions have the [Command] attribute. WITH the command attribute, client can't interact with the weapons. Error message pops the following: Trying to send command for object without authority.

So, there's 2 prefabs for each weapon. A ground prefab, that pretty much just holds a mesh, and an actionable prefab that equips to the player hands and has particle effects and what not. The GROUND prefab currently has Server Authority on the Network ID component enabled. The ACTIONABLE prefab has no authorities set. Enabling Server Authority on my GROUND prefabs allows my initial groundspawn to work. Otherwise, it spawns 2 prefabs on the ground.

On initial pickup: Server: As of right now, the server sees everything on it's side correctly. However, if the client tries to "pick up" a weapon, the server does not see the prefab on the ground get destroyed and doesn't see the gun snap to the client's equipzone (hands).

Client: Same vice versa, if server "picks up" a weapon, client does not see that prefab on the ground get destroyed, BUT the client can see the weapon snap to the server's hands.

For weapon swap: Server: Again, server sees what it's doing perfectly fine. BUT If client interacts, then no change is seen on server side.

Client: Client can see the prefab change for the server. (Server had an AK-47, now has an M416)

Here's the code! I apologize in advance if it's difficult to read.

 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking;
 
 public class WeaponSpawnLocation : NetworkBehaviour {
     public WeaponDatabase weaponDB;
     public Text displayText;
 
     [SyncVar(hook = "UpdateWeaponIndex")]
     private int weaponIndex;
 
     private GameObject weaponPrefabToBeDestroyed, weaponToDrop;
     private Weapon tempWeaponVariable, initialWeapon;
 
     private Transform equipZone;
     private bool weaponToGround = false;
 
     // Use this for initialization
     void Start()
     {
         if (isServer) OnWeaponSpawnStart();
         if (!isServer) UpdateWeaponIndex(weaponIndex);
 
         SpawnGameObjectToGround(initialWeapon.weaponPickupPrefab);
     }
 
     private void OnTriggerEnter(Collider other)
     {
         
 
         if(isLocalPlayer)
         {
             Debug.Log("Local player entered Trigger!");
         }
 
         if (other.tag == "Player")
         {
             displayText.enabled = true;
             displayText.text = "Press F to Equip " + initialWeapon.weaponName;
             equipZone = other.GetComponent<Gun>().firingFacingDirection;
 
         }
     }
 
     private void OnTriggerStay(Collider other)
     {
         if (other.tag != ("Player")) return;
         // If you have the same ammo type, display remaining ammo of said type
         if (initialWeapon.ammoType == other.GetComponent<Gun>().ammoType)
         {
             displayText.text = initialWeapon.ammoInReserves + initialWeapon.ammoInMagazine + " rounds of " + initialWeapon.ammoType;
         }
         // If ammo has been looted, read "No Ammo"
         else if (initialWeapon.ammoType == other.GetComponent<Gun>().ammoType && (initialWeapon.ammoInMagazine + initialWeapon.ammoInReserves) == 0)
         {
             displayText.text = "No ammo";
         }
         // If weapon names don't match, state a "Swap" message
         else if (initialWeapon.weaponName != other.GetComponent<Gun>().weaponName && CheckForWeapon.weaponEquipped)
         {
             displayText.text = "Swap " + other.GetComponent<Gun>().weaponName + " for " + initialWeapon.weaponName;
         }
         // If none of the above and player is empty handed, state a "Pickup" message
         else
         {
             displayText.text = "Press F to Equip " + initialWeapon.weaponName;
         }
 
         // Execute the following when the F key is pressed
         if (Input.GetButtonDown("Action"))
         {
             // If no weapon is equipped, pickup weapon and destroy the spawned original prefab
             if (!CheckForWeapon.weaponEquipped)
             {
                 CmdSpawnGameObjectToEquipZone(initialWeapon.weaponPrefab);
                 other.GetComponent<Gun>().weaponStats
 = initialWeapon;
                 CmdDestroyObject(weaponPrefabToBeDestroyed);
                 displayText.enabled = false;
                 CmdDestroyObject(gameObject);
             }
             else
             {
                 // Pick up ammo from location
                 if (initialWeapon.ammoType == other.GetComponent<Gun>().ammoType)
                 {
                     other.GetComponent<Gun>().ammoInReserves
 += initialWeapon.ammoInReserves;
                     other.GetComponent<Gun>().ammoInReserves
 += initialWeapon.ammoInMagazine;
                     initialWeapon.ammoInReserves -= initialWeapon.ammoInReserves;
                     initialWeapon.ammoInMagazine -= initialWeapon.ammoInMagazine;
                     //ammoPickedUp = true;
                 }
                 // Swap weapons
                 else if (initialWeapon.weaponName != other.GetComponent<Gun>().weaponName)
                 {
                     weaponToGround = true;
                     CmdSpawnGameObjectToGround(other.GetComponent<Gun>().weaponPickUpPrefab);
                     CmdDestroyObject(weaponPrefabToBeDestroyed);
                     weaponPrefabToBeDestroyed = weaponToDrop;
                     CmdDestroyObject(equipZone.GetChild(0).gameObject);
                     tempWeaponVariable = other.GetComponent<Gun>().weaponStats;
                     // Next two lines ensure the weapon that is dropped contains the ammo used/reserved
                     tempWeaponVariable.ammoInReserves = other.GetComponent<Gun>().ammoInReserves;
                     tempWeaponVariable.ammoInMagazine = other.GetComponent<Gun>().ammoInMagazine;
 
                     other.GetComponent<Gun>().weaponStats
 = initialWeapon;
                     other.GetComponent<Gun>().weaponStatsChecked
 = false;
                     CmdSpawnGameObjectToEquipZone(initialWeapon.weaponPrefab);
                     initialWeapon = tempWeaponVariable;
                 }
             }
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player")
         {
             displayText.enabled = false;
             equipZone = null;
         }
     }
 
     void SpawnGameObjectToGround(GameObject objectToSpawn)
     {
         GameObject instance = Instantiate(objectToSpawn, this.gameObject.transform.position,
             Quaternion.Euler(0, Random.Range(-180, 180), 90));
 
         if (isServer)
         {
             NetworkServer.Spawn(instance);
         }
  
         if (weaponToGround)
         {
             weaponToDrop = instance;
         }
         else
         {
             weaponPrefabToBeDestroyed = instance;
         }
     }
 
     [Command]
     void CmdSpawnGameObjectToGround(GameObject objectToSpawn)
     {
         GameObject instance = Instantiate(objectToSpawn, this.gameObject.transform.position,
             Quaternion.Euler(0, Random.Range(-180, 180), 90));
 
         if (isServer)
         {
             NetworkServer.Spawn(instance);
         }
 
         //RpcSpawnGameObjectToGround(instance);
 
         if (weaponToGround)
         {
             weaponToDrop = instance;
         }
         else
         {
             weaponPrefabToBeDestroyed = instance;
         }
     }
 
     [Command]
     void CmdSpawnGameObjectToEquipZone(GameObject objectToSpawn)
     {
         GameObject instance = Instantiate(objectToSpawn, equipZone);
 
         if (isServer)
         {
             NetworkServer.Spawn(instance);
         }
     }
 
     [Command]
     void CmdDestroyObject(GameObject objectToDestroy)
     {
         Destroy(objectToDestroy);
 
         NetworkServer.Destroy(objectToDestroy);
     }
 
     void OnWeaponSpawnStart()
     {
         UpdateWeaponIndex(Random.Range(0, weaponDB.weapons.Count));
     }
 
     void UpdateWeaponIndex(int _weaponIndex)
     {
         weaponIndex = _weaponIndex;
 
         initialWeapon = new Weapon(weaponDB.weapons[weaponIndex].weaponID, weaponDB.weapons[weaponIndex].weaponPrefab,
             weaponDB.weapons[weaponIndex].weaponPickupPrefab, weaponDB.weapons[weaponIndex].weaponName,
             weaponDB.weapons[weaponIndex].weaponPickupText, weaponDB.weapons[weaponIndex].weaponDamage,
             weaponDB.weapons[weaponIndex].weaponRecoil, weaponDB.weapons[weaponIndex].impactForce,
             weaponDB.weapons[weaponIndex].fireRate, weaponDB.weapons[weaponIndex].magazineCapacity,
             weaponDB.weapons[weaponIndex].ammoInMagazine, weaponDB.weapons[weaponIndex].ammoInReserves,
             weaponDB.weapons[weaponIndex].ammoType);
     } }

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

0 Replies

· Add your reply
  • Sort: 

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

160 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 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 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 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 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 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 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

make script target player that enters collider 0 Answers

Can't click gameobject when over another trigger? 1 Answer

Collider should trigger if another comes in 2 Answers

Activate Object with trigger and if leave then inactivate 2 Answers

Where to attach a script 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