• 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 /
  • Help Room /
avatar image
1
Question by coasternews · Dec 12, 2015 at 11:21 AM · multiplayerunity 5.2

Unet Client Spawning objects

I am having a problem with spawning objects on the client. When i am trying to spawn objects on with the host everything works fine and the client is able to see the spawned objects. But when I am trying to spawn an object with the client I get an error: ArgumentException: The thing you want to instantiate is null. I have been trying to fix this for a while now but I am unable to find the problem.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class SpawnObjects : NetworkBehaviour {
 
     [SyncVar]
     public GameObject building1;
     [SyncVar]
     public GameObject building2;
     public GameObject turret;
     GameObject clone;
 
     public Vector3 playerPos;
     Vector3 playerDirection;
     Quaternion playerRotation;
     int currentBuilding = 1;
     float timeElapsed = 0;
 
     // Use this for initialization
     void Start () {
         ClientScene.RegisterPrefab(building1);
         ClientScene.RegisterPrefab(building2);
 
     }
     
     // Update is called once per frame
     void Update () {
         timeElapsed += Time.deltaTime;
 
         if (Input.GetKeyDown(KeyCode.Keypad1) && timeElapsed < 120)
         {
             currentBuilding = 1;
         }
         else if (Input.GetKeyDown(KeyCode.Keypad2) && timeElapsed < 120)
         {
             currentBuilding = 2;
         }
 
         if(timeElapsed < 120)
         {
             PlayerShoot shoot = GetComponent<PlayerShoot>();
             shoot.timeElapsed = 0f;
             Player player = GetComponent<Player>();
             player.currentHealth = 100;
         }
         else
         {
             currentBuilding = 0;
         }
 
         if (Input.GetButtonDown("Fire1"))
         {
             building1 = new GameObject();
             building2 = new GameObject();
             playerPos = turret.transform.position;
             playerRotation = turret.transform.rotation;
             playerDirection = turret.transform.up;
             if (currentBuilding == 1)
             {
                 CmdNetworkSpawn(building1);
             }
             else if (currentBuilding == 2)
             { 
                 CmdNetworkSpawn(building2);
             }
         }
     }
 
     [Command]
     void CmdNetworkSpawn(GameObject test)
     {
         clone = Instantiate(test, playerPos - transform.forward * 30, playerRotation) as GameObject;
         if (isServer)
         {
             NetworkServer.Spawn(clone);
         }
         else if (isClient)
         {
             NetworkServer.SpawnWithClientAuthority(clone,base.connectionToClient);
         }
         
     }
 
 }
 
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
0
Best Answer

Answer by Thorious · Dec 12, 2015 at 11:07 PM

If I am understanding how they have it, it has to be in a Resource folder. This is what I am using to Instantiate an object.

invItem = "Prefabs/Items/" + invItem; GameObject lHandItem = Instantiate(Resources.Load(invItem, typeof(GameObject))) as GameObject;

Hope that helps.

Comment
Add comment · Show 5 · 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 coasternews · Dec 13, 2015 at 04:01 PM 0
Share

Thank you that worked. I am having another problem when trying to shoot the placed object I thought that it was linked to the placing problem. When I am trying to shoot the object from as the host the objects health goes down and the collider is removed when it's health drops below zero. But when I am trying to shoot the object with the client I get an error:NullReferenceException: Object reference not set to an instance of an object PlayerShoot.CmdObjectShot (RaycastHit hit, Int32 _damage) (at Assets/Scripts/PlayerShoot.cs:72). I am shooting with a raycast.

 [Client]
     void Shoot ()
     {
 
         Vector3 playerPos = turret.transform.position;
         Vector3 playerDirection = turret.transform.up;
 
         RaycastHit hit;
         if (Physics.Raycast(playerPos, playerDirection, out hit, 10000.0f))
         {
             Debug.Log(hit.collider.name);
             if (hit.collider.tag == PLAYER_TAG)
             {
                 CmdPlayerShot(hit.collider.name, weapon.damage);
             }
             else if (hit.collider.name == "Cube(Clone)" || hit.collider.name == "Cube2(Clone)")
             {
                 CmdObjectShot(hit, weapon.damage);
             }
         }
 
     }
 
     [Command]
     void CmdPlayerShot (string _playerID, int _damage)
     {
         Debug.Log(_playerID + " has been shot.");
 
         Player _player = Game$$anonymous$$anager.GetPlayer(_playerID);
         _player.TakeDamage(_damage);
     }
 
     [Command]
     void CmdObjectShot(RaycastHit hit, int _damage)
     {
         hit.collider.GetComponent<UpdateHealth>().TakeDamage(_damage);
     }
avatar image Thorious · Dec 13, 2015 at 11:10 PM 0
Share

Here is the code I use for shooting, this is attached to the player.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class Player_Shoot : NetworkBehaviour {
 
     private int damage = 25;
     private float range = 200;
     [SerializeField] private Transform camTransform;
     private RaycastHit hit;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         CheckIfShooting();
     }
 
     void CheckIfShooting()
     {
         if(!isLocalPlayer)
         {
             return;
         }
 
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
         {
             Shoot();
         }
     }
 
     void Shoot()
     {
         if(Physics.Raycast(camTransform.TransformPoint(0, 0, 0.5f), camTransform.forward, out hit, range))
         {
             //Debug.Log(hit.transform.tag);
 
             if(hit.transform.tag == "Player")
             {
                 string uIdentity = hit.transform.name;
                 CmdTellServerWhoWasShot(uIdentity, damage);
             }
 
             else if(hit.transform.tag == "Zombie")
             {
                 string uIdentity = hit.transform.name;
                 CmdTellServerWhichZombieWasShot(uIdentity, damage);
             }
         }
     }
 
     [Command]
     void CmdTellServerWhoWasShot (string uniqueID, int dmg)
     {
         GameObject go = GameObject.Find(uniqueID);
         go.GetComponent<Player_Health>().DeductHealth(dmg);
     }
 
     [Command]
     void CmdTellServerWhichZombieWasShot (string uniqueID, int dmg)
     {
         GameObject go = GameObject.Find(uniqueID);
         go.GetComponent<Zombie_Health>().DeductHealth(dmg);
     }
 
 }
 
avatar image coasternews Thorious · Dec 14, 2015 at 01:03 PM 0
Share

Thank you the player can now shoot the objects the only problem is that most of my spawned prefabs have the same name and when I try to change the name while spawning the objects I get the same error again.

 [Command]
     void CmdNetworkSpawn(GameObject test)
     {
         //clone = Instantiate(test, playerPos - transform.forward * 30, playerRotation) as GameObject;
         clone = Instantiate(Resources.Load("Cube", typeof(GameObject)), transform.position - transform.forward * 30, playerRotation) as GameObject;
         clone.transform.name = "Cube " + number;
         number++;
         clone.tag = "Cube";
         //Debug.Log(clone.tag);
         
         if (isServer)
         {
             NetworkServer.Spawn(clone);
         }
         else if (isClient)
         {
             NetworkServer.SpawnWithClientAuthority(clone,base.connectionToClient);
         }
         
     }
avatar image Thorious coasternews · Dec 14, 2015 at 02:59 PM 0
Share

Try this video subscription, its what you are looking for. https://www.youtube.com/watch?v=e$$anonymous$$kE7ki0XbA&feature=youtu.be

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

48 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

Related Questions

[UNET 5.2] Multiple local authority for 1 object? 0 Answers

islocalplayer stopping buttons working 0 Answers

How to accept Multiplayer EULA 2 Answers

button action in the same controller for local muiltiplayer,Instantiate button action for local multiplayer 1 Answer

Multiplayer - Error when rejoining room 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