Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 G_Force · Jun 11, 2018 at 06:43 AM · networkingrespawnobject-reference-error

Network Respawn Object not set...

I have been trying for ages to get this script to respawn the client on a respawn point but it just doesnt work. The debug log keeps giving me the NullReferenceException: Object reference not set to an instance of an objectPlayerController.CmdRespawn

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Networking;
 
 public class PlayerController : NetworkBehaviour {
 
 
 
 
     public GameObject playerCamera;
     public GameObject turret;
     public GameObject gun;
     public GameObject barrleGun;
     public GameObject light;
     public GameObject tracer;
 
     private Animator gunanim;
 
     private float reloadtime;
     private float reloadtimer;
     private float hitdamage = 10;
     [SyncVar] public float hp = 100;
     private GameObject healthbar;
     private RectTransform healthbars;
     private RectTransform reload;
 
     private float vinput;
     private float speed;
     private float rotationspeed;
     private float camdir;
 
     private NetworkStartPosition[] spawns;
 
     void Start()
     {
         #region cam
             if (isLocalPlayer == true)
             {
                 playerCamera.SetActive(true);
                 light.SetActive(true);
             }
             else
             {
                 playerCamera.SetActive(false);
                 light.SetActive(false);
             }
         #endregion
         #region Variables
             speed = 10;
             rotationspeed = 90;
 
             gun.transform.SetParent(turret.transform);
             gunanim = barrleGun.GetComponent<Animator>();
             barrleGun.transform.SetParent(turret.transform);
             tracer.transform.SetParent(turret.transform);
             tracer.SetActive(false);    
             
             reloadtime = 3;
             reloadtimer = 0;
             camdir = this.transform.rotation.y;
 
             healthbar = GameObject.Find("Current health");
             healthbars = healthbar.GetComponent<RectTransform>();
 
             reload = GameObject.Find("CurrentReload").GetComponent<RectTransform>();
 
             if (isLocalPlayer)
             {
                 spawns = FindObjectsOfType<NetworkStartPosition>(); 
                 
             }
 
 
         
 
         #endregion
     }
 
     void Update()
     {
         if (isLocalPlayer)
         {
             this.healthbars.localScale = new Vector3(this.hp / 100, 1);
             #region movement
             #region Forward/Backwards
             vinput = Input.GetAxis("Vertical") * Time.deltaTime * speed;
                     this.transform.Translate(Vector3.forward * vinput);
                 #endregion
                 #region rotation
                     float turn = Input.GetAxis("Horizontal") * rotationspeed * Time.deltaTime;
                     this.transform.Rotate(0f, turn, 0f);
                     
             #endregion
             #endregion
             #region Turret
             #region Rotation
             
                     Ray cameraRay = playerCamera.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                     Plane ground = new Plane(Vector3.up, Vector3.zero);
                     float raylenght;
 
                     if (ground.Raycast(cameraRay, out raylenght))
                     {
                         Vector3 pointToLook = cameraRay.GetPoint(raylenght);
                         Debug.DrawLine(turret.transform.position + Vector3.forward * 1.5f, pointToLook);
                         turret.transform.LookAt(new Vector3(pointToLook.x, turret.transform.position.y, pointToLook.z));
 
                         
                     }
 
             #endregion
             #region gun
             
             if (reloadtimer <= 0)
             {
                 reloadtimer = 0;
                 reload.localScale = new Vector3(0, 0);
             }
             else
             {
                 reloadtimer -= 1 * Time.deltaTime;
                 reload.localScale = new Vector3(reloadtimer / 3f, 1);
             }
 
             if (Input.GetMouseButtonDown(0))
             {
                 if (reloadtimer <= 0)
                 {
                     tracer.SetActive(false);
                     RaycastHit hit;
                     if (Physics.Raycast(gun.transform.position, gun.transform.forward, out hit))
                     {
 
                         Debug.Log(hit.transform.name);
                         if (hit.transform.CompareTag("Player"))
                         {
                             CmdDamagePlayer(hit.transform.gameObject, hitdamage);
 
                             
 
                             print(hit.transform.GetComponent<PlayerController>().hp);
                         }
                     }
                     gunanim.SetTrigger("Fired");
                     reloadtimer = reloadtime;
                     tracer.SetActive(true);
                 }
             }
             gunanim.SetFloat("reload", reloadtimer);
             
             #endregion
             #endregion
             #region cam
             playerCamera.transform.rotation = Quaternion.Euler(90, 0, camdir);
             #endregion
             
             #region features
             
             if (this.hp <= 0)
             {
                 if (isServer)
                 {
                     if (this.gameObject != null)
                     {
                         RpcRespawn(this.gameObject);
                     }
                     else
                     {
                         print("Nope");
                     }
                 }
                 else
                 {
                     if (this.gameObject != null)
                     {
                         CmdRespawn(this.gameObject);
                     }
                     else
                     {
                         print("Nope");
                     }
                 }
             }
             #endregion
         }
     }
 
     /*
     public void TakeDamage(RaycastHit hit,float damage)
     {
 
         if (!isServer)
         {
             return;
         }
         hit.transform.GetComponent<PlayerController>().hp -= damage;
         
         
         if (hit.transform.GetComponent<PlayerController>().hp <= 0)
         {
             hit.transform.GetComponent<PlayerController>().RpcRespawn(hit.transform.gameObject);
         }
     }
     */
 
     [ClientRpc]
     void RpcRespawn(GameObject gmbj)
     {
         if (gmbj != null)
         {
             Vector3 spawnpoint = spawns[Random.Range(0, spawns.Length)].transform.position;
             gmbj.transform.position = spawnpoint;
             gmbj.GetComponent<PlayerController>().hp = 100f;
         }
         else
         {
             print("Nope");
         }
         
     }
     [Command]
     void CmdRespawn(GameObject gmbj)
     {
         
             Vector3 spawnpoint = spawns[Random.Range(0, spawns.Length)].transform.position;
             gmbj.gameObject.transform.position = spawnpoint;
             gmbj.gameObject.transform.GetComponent<PlayerController>().hp = 100f;
         
     }
 
 
     [Command]
     void CmdDamagePlayer(GameObject hit, float damage)
     {
         float ehp = hit.transform.GetComponent<PlayerController>().hp;
         hit.transform.GetComponent<PlayerController>().hp -= damage;
         if (hit.transform.GetComponent<PlayerController>().hp <= 0)
         {
             /*
             if (hit == isServer)
             {
                 RpcRespawn(hit.gameObject);
             }
             else
             {
                 CmdRespawn(hit.gameObject);
             }
             */
         }
     }
     
 }

NullReferenceException: Object reference not set to an instance of an object PlayerController.CmdRespawn (UnityEngine.GameObject gmbj) (at Assets/Scripts/PlayerController.cs:228) PlayerController.InvokeCmdCmdRespawn (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader) UnityEngine.Networking.NetworkIdentity.HandleCommand (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:617) UnityEngine.Networking.NetworkServer.OnCommandMessage (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1281) UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:469) UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:425) UnityEngine.Networking.NetworkConnection.TransportReceive (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:576) UnityEngine.Networking.NetworkServer.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:738) UnityEngine.Networking.NetworkServer+ServerSimpleWrapper.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1867) UnityEngine.Networking.NetworkServerSimple.HandleData (Int32 connectionId, Int32 channelId, Int32 receivedSize, Byte error) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:384) UnityEngine.Networking.NetworkServerSimple.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:247) UnityEngine.Networking.NetworkServer.InternalUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:691) UnityEngine.Networking.NetworkServer.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:642) UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1090)

Edit: I have updated the Script so that you can see the whole PlayerController

IMPORTANT I have fixed the issue with a lot of help from Captain_Pineapple

I just created a normal Respawn void wich Placed the Player in the right position and used the Command and ClientRpc to restore the health to 100

Comment
Add comment · Show 8
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 Captain_Pineapple · Jun 08, 2018 at 11:19 AM 0
Share

Hey there,

just a guess: you send the respawn rpc to ALL players but only the instance of the local player actually fills the spawnsvariable with a value. So you should get a nullreference on non-local-player characters when trying to access the spawns array.

avatar image G_Force Captain_Pineapple · Jun 08, 2018 at 12:15 PM 0
Share

Well thats Interresting but i don't think so since it only gives me the Error massage when trying to find the gmbj so the player Object p.s. But could you tell me how to do that anyway

avatar image Captain_Pineapple G_Force · Jun 09, 2018 at 11:34 AM 0
Share

can you post a bit more of your code and especially the full error message containing the explicit line numbers?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

124 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

Related Questions

unity3d multiplayer respawn 2 Answers

networking, online FPS player invisible to others after respawn 0 Answers

Networking how to correctly respawn players 2 Answers

Spawning a new player instance (UNET) 3 Answers

Unity networking tutorial? 6 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