• 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
Question by mEX · May 17, 2011 at 12:57 PM · instantiatenetworkserverclientduplicate

Network.Instantiate fired from client AND server

Hi,

I'm trying Unitys network functionality for the first time. My current problem is the following: A player can Network.Instantiate a building that will automatically spawn units for $$anonymous$$m. t$$anonymous$$s works fine, the building is created for both client and server.

But when the building spawns the unit the Network.Instantiate for t$$anonymous$$s unit is fired by both the server and the client (as the building exists on both) and therefore i'll have two units. If I GameObject.Instantiate the unit, i'll end up with one spawned unit on each client and server, but i guess that i'll have virtually two units then, because they are not connected via network. I'd like to try t$$anonymous$$s in a non authoritive server approach first, if its possible. I'm not sure wether t$$anonymous$$s piece of code helps. How do I fix t$$anonymous$$s?

void FixedUpdate() { if (spawnsCreep == null) { return; }

 timer -= Time.deltaTime;
 if (timer <= 0)
 {
     Spawn();
     timer = spawnTimer;
 }

}

void Spawn() { GameObject offspring = Network.Instantiate(spawnsCreep, GetComponent<Transform>().position, Quaternion.identity, 0) as GameObject; int id = GetComponent<CombatBehaviour>().GetPlayerID(); offspring.GetComponent<CombatBehaviour>().SetNetworkPlayerID(id); }

Note: Both the units and buildings NetworkView are already added in the prefabs.

Edit: Of course after asking the Question, a quick and dirty solution came to my mind:

if (Network.isClient)
    {
        return;
    }

Seems quick and dirty, works for the moment. But i'm not sure if its the optimal way to build on.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · May 17, 2011 at 01:53 PM

If the script is only responsible for spawning the units, just disable or remove the script on all other clients (server included). Just check in Start if you are the owner of t$$anonymous$$s building.

if (!networkView.isMine)
{
    Destroy(t$$anonymous$$s);
    // or
    enabled = false;
}

if you still need some functionality of t$$anonymous$$s script you can restrict the spawning to the owner just the same way:

void Spawn()
{
    if (!networkView.isMine)
        return;
    [...]
Comment
mEX
MestR

People who like this

2 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 MestR · Aug 29, 2012 at 07:12 AM 0
Share

Sorry to bring this up again, but if I would instantiate something by using this method that I only want to be used on the client instantiating it and the server, would traffic still be sent to the other clients as well (and just be ignored)?

avatar image Bunny83 · Aug 30, 2012 at 02:07 AM 0
Share

@MestR: Sorry, but i'm not sure what you mean ;) If you Network.Instantiate an object, it will be instantiated on all peers. All peers have to have an object with this networkviewID or you get massive errors. In theory you can delete a "networked" object only on one client, but the updates will still be sent and causes errors because there's no receiver.

I've seen your question about SetScope, i will answer there since it seems related.

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

2 People are following this question.

avatar image avatar image

Related Questions

Network.Instantiate and the Trusted Client problem 0 Answers

Level Instantiation running on Server and client 0 Answers

Network.instantiate for specified clients? 1 Answer

RPC Call Mix Up Issues 0 Answers

No state update from client (Network) 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