Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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 him. this works fine, the building is created for both client and server.

But when the building spawns the unit the Network.Instantiate for this 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 this in a non authoritive server approach first, if its possible. I'm not sure wether this piece of code helps. How do I fix this?

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
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
2
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 this building.

if (!networkView.isMine)
{
    Destroy(this);
    // or
    enabled = false;
}

if you still need some functionality of this script you can restrict the spawning to the owner just the same way:

void Spawn()
{
    if (!networkView.isMine)
        return;
    [...]
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 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

@$$anonymous$$estR: 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.

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

Network side instantiating 1 Answer

OnConnectedToServer not called when testing on same machine 0 Answers

How can I spawn a GameObject on all players in my multiplayer game? 0 Answers

Smooth network communication like Unity remote 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