Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Slences · Jul 21, 2015 at 01:38 PM · photoninsantiate

Photon Network variables on spawn not set on client

I have the following method to spawn a GameObject (which is being fired on RMB).

 void SpawnProjectile(string SpellName)
     {
         Vector3 spawnLocation = transform.Find("ProjectileSpawnLocation").transform.position;
         
         GameObject go = (GameObject)PhotonNetwork.Instantiate(SpellName, spawnLocation, transform.rotation, 0);
         
         Projectile projectile = go.GetComponent<Projectile>();
         
         projectile.totalRange = projectile.BaseRange + spell.getSpellLevel();
         projectile.totalDamage = projectile.BaseDamage + spell.getSpellLevel();
     
         go.name = SpellName;
         projectile.Instigator = gameObject;
         
         projectile.enabled = true;
     }

This works fine for the master client, however, when I do this on a client, variables aren't right and it gives errors like e.g. Null reference on Instigator, because I'm checking on the OnCollisionEnter if the Instigator is not equal to the collision object.

What can I do to make this work for all connected clients? Thanks in advance!

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
Best Answer

Answer by alexi123454 · Jul 21, 2015 at 01:54 PM

You're only setting the variables on the master client, because this function is only being called on the master client. To send the same information to the rest of the players in the game, you're going to have to send them a network message (an RPC).

You can create an RPC function by putting the [PunRPC] tag above it, and then calling it on the master client by calling GetComponent().RPC("rpcFunctionName", PhotonTargets.Others, spellName, totalRange, totalDamage, SpellName, instigator.GetComponent().owner), or something along those lines.

Just send the variables you want to set as arguments through the RPC call, and all the other clients (or all clients including the master client if you use PhotonTargets.All instead) will have all of the variables, and can set the projectile's variables themselves.

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 Slences · Jul 21, 2015 at 03:00 PM 0
Share

I tried using a RPC call before (obviously in a wrong way), there is one thing I dont get though. I used a new method purely to set variables for the projectile via RPC, this works fine, but I don't quite get how to assign the Instigator (owner) to the projectile since I can't pass Transform and GameObject as an argument.

avatar image alexi123454 · Jul 21, 2015 at 03:03 PM 1
Share

Ins$$anonymous$$d of passing the transform and the gameObject, just pass the instigator.GetComponent().owner. This is an int that identifies a PhotonPlayer. Once on the client side, you can search through the players in the scene, and see if their owner matches the owner received in the RPC call. If it does, set the instigator to that gameObject!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Server for Unity Networking 0 Answers

PhotonNetwork.ConnectUsingSettings("DevBuild6b") returns NullReferenceException 0 Answers

photon player movement 1 Answer

Photon Unity - Only receiving one value. 3 Answers

NullReferenceException: Object reference not set to an instance of an object 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