• 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
Question by Chexxor · Dec 11, 2017 at 08:03 PM · networkingmultiplayerreferencespawningclient

Spawning GameObject on network, locally change it only on the client that spawned it.

What I want to do: I want a client that spawned as a "Player" to be able to spawn different Vehicles, in the example I have a car and a plane class to choose from. Both vehicle prefabs have specific scripts, one global script that will be enabled on that GameObject for everyone, but also a local script that will ONLY be enabled for the local player that was responsible for spawning that vehicle in the first place. Similarily I also have a camera gameobject attached to each vehicle prefab that is inactive by default that needs to be turned on.

Problem: I can't find a way that let's the client affect the gameobject after it's spawned. At best I only got "isLocalPlayer" to work for the host player, and that was when I tried putting isLocalPlayer inside the [command] method for spawning...

 class MyPlayer : NetworkBehaviour
 {
     public const int CAR = 0;
     public const int PLANE = 1;
 
     public GameObject carPrefab;
     public GameObject planePrefab;
     public Transform spawn;
 
     public GameObject currentVehicle;
 
     void Update()
     {
         if (!isLocalPlayer)
             return;
         if (Input.GetButtonDown("Spawn Car"))
             CmdSpawn(CAR);
         if (Input.GetButtonDown("Spawn Plane"))
             CmdSpawn(PLANE);
     }
 
     [Command]
     public void CmdSpawn(int vehicle)
     {
         GameObject prefab = (vehicle == CAR ? carPrefab : planePrefab);
         GameObject newVehicle = (GameObject)Instantiate(prefab, spawn.position, spawn.rotation);
         NetworkServer.Spawn(newVehicle);
     }
 }
 
 class Plane : NetworkBehaviour
 {
     public LocalFlightControls local;
     public GlobalFlightControls global;
     public new GameObject camera;
 
     private void Start()
     {
         if (isLocalPlayer)
         {
             local.enabled = true;
             camera.SetActive(true);
         }
     }
 }

In this example, isLocalPlayer never gets recognized in the start loop, since any player has a Network Identity, but the vehicles have their own Identities in order for Network Transform to work. I have tried a lot of searching and I'm honestly quite clueless. I also want to somehow set the player's "currentVehicle" variable to reference the vehicle that is currently in use, so that I can destroy it when I want to re-spawn or spawn another one

Comment

People who like this

0 Show 0
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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

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

Need help for Player Spawning and Player speed control slider!! 2 Answers

Failed to spawn server object, assetid=... 1 Answer

Unity Photon Handle Match Data 0 Answers

What are some best practices for static variables in multiplayer? 0 Answers

Setting parent on spawned object Unet 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