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 /
  • Help Room /
avatar image
0
Question by KristophV · Mar 25, 2016 at 07:37 AM · c#networkinglist

Adding object to a list does not work on non-host client

I'm making a grid-based turn-based strategy game with multiple units. Right now you can spawn units by pressing a key, which works fine. On the host client, spawning a unit adds that unit to the 'units' list, so that it can be selected/moved/etc.

However, if you spawn a unit on a non-host client, it's not added to the 'units' list for some reason. So, it is spawned, but it's impossible to select or move. Instead it just gives "NullReferenceException: Object reference not set to an instance of an object."

The list of units is in the Game Manager script:

 public static GameManager instance;
 public List<Unit> units = new List<Unit>();

There's a Player script which handles spawning:

 [Command]
 public void CmdSpawn()
  {
         obj = (GameObject)Instantiate(GameManager.instance.InfantryPrefab, transform.position, Quaternion.identity);
 
         newInf = obj.GetComponent<Infantry>();
         newInf.gridPosition = new Vector2(transform.position.x + Mathf.Floor(GameManager.instance.mapSize / 2), -transform.position.z + Mathf.Floor(GameManager.instance.mapSize / 2));
   
         // spawn on the clients
         NetworkServer.Spawn(obj);
 
         // adding newInf to the list of units
         GameManager.instance.units.Add(newInf);
     }

 public virtual void Update () {
  if (Input.GetKeyDown(KeyCode.J) && isLocalPlayer)
     {  
         CmdSpawn();
     }
  }

Is there a way to make it so the spawned units are added to the 'units' list on the non-host client as well?

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

68 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

Related Questions

Day/Night cycle turns black at night and how to Serialize it rightly? 0 Answers

Photon Networking - How to move all players from game scene to current room scene? 0 Answers

Calling Bool in Network Command Not Working 0 Answers

How to choose different teams one by one 1 Answer

Mobile Interaction 0 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