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 AdmiralSnuggles · Dec 11, 2013 at 08:19 PM · instantiatephotonreferencenullexception

NullReferenceException on Photon Unity RPC Call

Hey guys, been trying to figure this out for way to long and it's starting to get me down.

I'm working on a multiplayer game where the players can join at any time and take turns in stages. Here's what I believe is the offending code:

 void OnJoinedRoom(){
 
         myPlayerNumber = (int)PhotonNetwork.playerList.Length;
 
         xPos = (float)PhotonNetwork.playerList.Length * 0.2f;
         player = PhotonNetwork.Instantiate("Prefabs/Knight", new Vector3(-0.2f - xPos, -0.81f, 0), 
                                                       Quaternion.identity, 0);
 
         // game logic: if this is the only player, it's our turn
         if (PhotonNetwork.playerList.Length == 1){
             playerWhosTurnItIs = PhotonNetwork.player.ID;
             meanCrab = PhotonNetwork.Instantiate("Prefabs/EnemyMeanCrab", new Vector3(1.2f, -0.61f, 0), 
                                                  Quaternion.identity, 0);
         }
     }

When it's the player's turn they are allowed to press a button and attack the spawned enemy. The following function is called within a coroutine I have that checks if it is the player's turn. It works for my player 1, but when it is the other player's turns it throws a nullReferenceException (I believe this is on the meanCrab.transform.position)

     player.GetComponent<WeaponController>().photonView.RPC("Attack", PhotonTargets.All, 
                                                                meanCrab.transform.position);

I guess what I don't understand here is why it's throwing that exception. meanCrab has a Photon View so all other players should be able to access this object with an RPC, right?

player is the player who's turn it is, BTW. I can run this without the call to meancrab.transform.position and it cycles through the multiplayer turns correctly. If I instantiate my meancrab enemy object outside of the if(Photonnetwork.playerList.Length ==1) snippet, it will create a new meancrab for each player, but I only want one for all the players.

What am I doing wrong here? Thanks!

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
0

Answer by specterdragon · Jan 20, 2014 at 01:35 AM

I may be off as I'm new to Photon, but it looks like there's a couple of issues. You're trying to call the RPC on the player not the meanCrab. It also looks like there's a syntax error (unless there's more than one way to call an RPC) because you never get the PhotonView component

If this is coming from the player, then the player needs access to a game object that represents the meanCrab, which it looks like you have.

GameObject.Find("meanCrab").GetComponent(),RPC ("Attack", PhotonTargets.All, meanCrab.transform.position);

Or something along those lines. Again, I could be way off.

If the meanCrab is going to be synced, then you'll need a PhotonView on it anyhow, so what about using OnPhotonSerializeView? Would that work for what you need?

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

17 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

Related Questions

I am getting the error of NullReferenceException? 1 Answer

Instantiate() as GameObject = null reference 1 Answer

NullReferenceException Object reference error 1 Answer

Despawner script returns null reference exception. 0 Answers

Why is the Object reference is empty ? 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