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 edve98 · Oct 15, 2014 at 05:08 PM · c#networkingdebug

Cant transfer object references through the network?

I am really sorry for asking this here, but I can't find the error in my code for some time now. I get this error when somebody respawns(both the server host and client)

This is the error that I get:

NullReferenceException: Object reference not set to an instance of an object Spawning.Respawn () (at Assets/Scripts/Spawning.cs:47)

This is Spawning.cs:

 using UnityEngine;
 using System.Collections;
 
 public class Spawning : MonoBehaviour {
 
     private bool hasSpawned = false;
 
     GameObject[] spawnPoints;
 
     static public GameObject player;
 
     private int RN;
 
 
 
     void Start(){
 
         if(!networkView.isMine){
 
             enabled = false;
 
         }
         else{
 
             spawnPoints = GameObject.FindGameObjectsWithTag("Respawn");
 
         }
 
     }
 
 
 
     [RPC]
     void Respawn(){
 
         if(player != null){
 
             Network.Destroy(player);
 
         }
 
         RN = Random.Range(0, spawnPoints.Length);
 
 
 
         Network.Instantiate(Resources.Load("Helicopters/Helicopter"), spawnPoints[RN].transform.position, spawnPoints[RN].transform.rotation, 5);
 
     }
 
 
 
     void OnGUI(){
 
         if(Network.isClient || Network.isServer){
 
             if(hasSpawned == false && GUI.Button(new Rect(50f, 25f, 150f, 30f), "Spawn")){
                 
                 Debug.Log("Spawning player...");
                 
                 networkView.RPC("Respawn", RPCMode.AllBuffered);
                 
                 hasSpawned = true;
                 
             }
             
             if(hasSpawned == true && GUI.Button(new Rect(50f, 25f, 150f, 30f), "Respawn")){
                 
                 Debug.Log("Respawning player...");
                 
                 networkView.RPC("Respawn", RPCMode.AllBuffered);
                 
             }
 
         }
 
     }
 
 }

Any ideas?

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

Answer by KMKxJOEY1 · Oct 15, 2014 at 06:42 PM

Either your spawnpoints are not set up properly or there is no prefab to load from at that resource location.

Comment
Add comment · Show 3 · 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 edve98 · Oct 15, 2014 at 06:46 PM 0
Share

I think neither of these are true, because when there is only a server hosting player, everything works fine

avatar image Bunny83 · Oct 16, 2014 at 02:33 AM 1
Share

@edve98: You think neither is true? To quote Under Siege 2: "Assumption is the $$anonymous$$OTHER of all f*** ups"

Since the error seems to come from your Network.Instantiate line there are only two possible reasons like $$anonymous$$$$anonymous$$$$anonymous$$xJOEY1 said.

Either

  • there's no asset called "Helicopter" in the "Helicopters" folder inside a Resources folder

  • or your spawnpoints array contains null-elements or is itself null.

By looking at your RPC functions and your Start function it's quite easy to spot. You only initialize the spawnPoints array if this object is owned by you. However you send an RPC to all other players which will also call the Respawn method on their side (well, that's the point of a remote procedure call). This actually doesn't make any sense since you use Network.Instantiate which already uses an RPC to spawn the object on all clients. Calling Respawn on a foreign peer will of course fail since you don't initialized the spawnPoints array there.

You should recapitulate the whole respawn procedure and think about which method needs to be called where and by whom.

ps: since you have a player variable which probably holds a reference to the player object, which you see$$anonymous$$gly destroy before respawning, i just wonder if you actually set the new player object somewhere? $$anonymous$$aybe you want to assign the return value of Network.Instantiate to it?

avatar image edve98 · Oct 16, 2014 at 05:36 PM 0
Share

@Bunny83 wow, now I can't believe I have actually done this, please convert your comment to answer.

P.s. I think I will write down the words "Assumption is the $$anonymous$$OTHER of all f*** ups" and I will always look at them when something goes wrong :P

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

29 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

Related Questions

Not able to convert GetComponent ().netId to a string. 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Server still sending Packets to inactive clients and throwing errors. How can I stop it? 0 Answers

Character not rotating 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