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 Bioman75 · Jul 26, 2020 at 08:31 AM · networkingphotonspawning

How to Instance GameObjects Over the Pun Network

Hello, I have a script that spawns enemies in a wave based fashion but I am having trouble figuring out how to instance my enemy gameobjects over the pun network. I already have the Photon view and everything on the gameobject but i'm not sure how to actually instance it over the network so other player can see the enemies. My current code for spawning enemies is this:

 GameObject instance = (GameObject)Instantiate (zombieEnemy2, near [spawnPointIndex].position, near [spawnPointIndex].rotation);
                 instance.transform.Rotate (Vector3.up, Random.Range (0f, 360f));
Comment
Add comment · Show 1
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 Llama_w_2Ls · Jul 26, 2020 at 08:48 AM 0
Share

When spawning a prefab, you need to use PhotonNetwork.Instantiate. Heres how it works:`GameObject myPlayer = PhotonNetwork.Instantiate(playerPrefab.name, new Vector3(0f, 0.5f, 0f), Quaternion.identity, 0);`

Pay attention to how you use playerPrefab.name. You need to assign the name of the prefab. Also make sure that a photonView and photonTransformView is assigned to the enemy prefab. If you want more guidelines on this, check out the official PUN networking tutorial documentation.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Captain_Pineapple · Jul 26, 2020 at 08:51 AM

well there are multiple questions that come up here: what do you do with the zombies? Are they also synced by position? if yes they have a PhotonView and you must use PhotonNetwork.Instantiate.

If you use some other sync method (which i'd highly recommend) the your spawner script needs some kind of RPC. So your spawn function SpawnZombie gust gets an extra argument, say int spawnIndex and the one player (most probably the master client) should call the function:

 photonView.RPC("SpawnZombie", RpcTarget.All, hereGoesTheNumberOfYourSpawn);

This will the call the named function on ALL players currently connected to your room.

Assuming that your function is:

 [PunRPC]
 public void SpawnZombie(int spawnIndex)
 {
     GameObject instance = (GameObject)Instantiate (zombieEnemy2, near [spawnIndex].position, near [spawnIndex].rotation);
 }

As you might notice i removed the rotate part from the code since that would not be synced with the current version. So for the random rotation you have to add another argument to the RPC so that the random rotation is decided once for all players by the player why calls the RPC.


Let me know if that helped/if something was unclear.

Comment
Add comment · 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
0

Answer by Bioman75 · Jul 27, 2020 at 12:34 AM

Okay after tweaking some things your answer works. Thank you :)

Comment
Add comment · 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

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

187 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

Related Questions

Photon Network, how to spawn FPSController from Unity Standard Assets? 2 Answers

Photon Networking: How to spawn my players at the another places? 2 Answers

Unity PhotonNetwork globally enable a component 1 Answer

Only host client is able to spawn objects 0 Answers

[Photon]Player vs. Player Collision 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