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 MilesAway1980 · Nov 23, 2015 at 12:47 PM · gameobjectnetworkinginstantiatenullsync

[SyncVar] GameObject Instantiate returns null.

Hi all,

I'm working on converting a local multiplayer game (all players share the same screen) to a networked multiplayer game. This is in Unity 5.2, so it's using the most up to date stuff.

The issue I'm running into is when I start trying to sync variables across the network.

The way my players currently work is there is an array of GameObjects that contain different space ships (so its random which one they get). When the player spawns, the player's GameObject ship randomly gets instantiated as one of the space ship GameObjects from the array. Afterward, the ship is given a follow camera component, and a few other odds and ends. It works great locally.

However, I'm immediately running into a problem when I try to sync it over the network. If I try to [SyncVar] the GameObject, the instantiation returns Null. It's an immediate show stopper and I don't know how to get around it. I want the ship that the players use to all be synced.

This is a summarized section of the code where I'm running into the problem:

 public static class AllShips {
   GameObject[] shipList;
   public static GameObject[] getShipList() {
     if (shipList == null) {
       shipList = new GameObject[10];
     }
     for (int i = 0; i < shipList.Length; i++) {
       shipList[i] = (GameObject)Resources.Load("Prefabs/Ships/Ship_" + (i + 1), typeof(GameObject));
     }
     return shipList;
   }
 }

 public class Player : NetworkBehavior {
   [SyncVar] GameObject ship;
   void Awake() {
     InitState();
   }

   [Server]
   void InitState() {
     GameObject[] shipList = AllShips.getShipList ();
     int whichShip = Random.Range (0, shipList.Length - 1);
     ship = (GameObject)Instantiate (shipList [whichShip]);

     ship.name = "Player Ship";
     ship.transform.parent = transform;

     Camera cam = ship.AddComponent<Camera>();
     
     position = new Vector2(
       Random.Range (-1000, 1000),
       Random.Range (-1000, 1000)
     );
     ship.transform.position = position;
   }
 }

As said, all of that works great without the [SyncVar], but as soon as I try to sync it, the Instantiate returns Null instead of a new prefab GameObject. I'm not sure what to do about it or why it's happening.

FWIW, all of the other components, such as NetworkIdentity, etc. have been added to the player object that spawns.

Ideas?

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

Answer by meat5000 · Nov 23, 2015 at 04:05 PM

You can't SyncVar a GameObject. If you could itd be called SyncGO :)

Use SyncVar for basic typed variables only. Instantiate the object on the Server then call Spawn()

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 MilesAway1980 · Nov 23, 2015 at 04:18 PM 0
Share

Ahhh... That makes sense. I didn't even think about that hard enough.

However, why does this seem to work?

 struct CharacterInfo {
   GameObject ship;
 }  
 ...
 ...
 ...
 [SyncVar] CharacterInfo charInfo;
 charInfo.ship = (GameObject)Instantiate (shipList [whichShip]);

I was playing around with that last night and got a bit further. If nothing else, it didn't return null and I was able to get some progress.

Either way, good to know about Spawn(). Been program$$anonymous$$g for a lotta years, but this is only my second day ever messing with networking, so it's all new territory!

avatar image meat5000 ♦ MilesAway1980 · Nov 24, 2015 at 10:25 AM 0
Share

Its all in here. It states that you can SyncVar user defined structs, which I suppose could perfectly well contain a GameObject reference. The trouble is that the sync is applied as "$$anonymous$$onolithic Updates" as stated in the docs, not incremental changes, when using structs.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Every NetworkViewID is 0? 0 Answers

Can a Network.Instantiate()'d object be Object.Destroy()'d? 0 Answers

How to spawn gameobject from host to all clients 1 Answer

Syncing/Instantiating a LineRenderer on Network 2 Answers

check if an instantiated opject is null 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