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 reimeguroinferno_unity · Apr 14, 2018 at 02:43 AM · networkingnullreferenceexception

Null Reference Exception when using if(!islocalplayer) destroy

So Im a complete newbie in networking, im using a network manager. I added a destroy if not local player.

However for some reason I get a null reference exception. The network identity is set to local player authority and I added the player prefab, ticked spawn automatically in the network manager. Here's my full code:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.Networking;
 using UnityEngine;
 
 public class player_controller : NetworkBehaviour {
     //private int DebugComment;
     private int frame;
 
     public bool onVehicle = false;
     public bool nearPlanet = false;
     public float speed;
     private Vector3 moveDirection;
     public float mouseSensitivity;
     public float clampAngle;
     //public Camera cameraOBJ;
     //public Camera minimapcameraOBJ;
     Animator droidAnim;
     Rigidbody PlayerRB;
     Transform mainCam;
     Transform miniCam;
     public Vector3 offset;
     private float rotationY = 0.0f; // rotation around the up/y axis
     private float rotationX = 0.0f; // rotation around the right/x axis
 
     void Start()
     {
         /*if (!isLocalPlayer)
         {
             destroy(this);
             return;
         }*/
         mainCam = GameObject.Find("FirstPersonCamera").GetComponent<Transform>();
         miniCam = GameObject.Find("MinimapCamera").GetComponent<Transform>();
         offset = new Vector3(0f, 1000f, 0f);
         PlayerRB = GetComponent<Rigidbody>();
         Vector3 rotation = transform.localRotation.eulerAngles;
         rotationY = rotation.y;
         rotationX = rotation.x;
         //Camera cameracopy = Instantiate<Camera>(cameraOBJ);
         //Camera minimapcameracopy = Instantiate<Camera>(minimapcameraOBJ);
     }
 
 
 
 
 
     void Update()
     {
         Debug.Log("isLocalPlayer == " + isLocalPlayer);
         //camera
         Vector3 camPos = this.transform.position;
         mainCam.transform.position = camPos;
         Vector3 miniCamPos = this.transform.position + offset;
         miniCam.transform.position = miniCamPos;
         //DebugComment++;
 
         //Outer space
         if (onVehicle == false && nearPlanet == false)
         {
 
             //rotate
                 float mouseX = Input.GetAxis("Mouse X");
                 float mouseY = -Input.GetAxis("Mouse Y");
 
                 rotationY += mouseX * mouseSensitivity * Time.deltaTime;
                 rotationX += mouseY * mouseSensitivity * Time.deltaTime;
 
                 rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
 
                 if(rotationX < -30)
                 {
                     rotationX = -30;
                 }
 
                 Quaternion localRotation = Quaternion.Euler(rotationX, rotationY, 0.0f);
                 transform.rotation = localRotation;
                 //end of rotate
 
 
 
                 //move
                 moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
                 moveDirection = moveDirection.normalized * speed;
                 if (Input.GetButton("Jump"))
                 {
                     moveDirection.y = speed;
                 }
                 if (Input.GetKey(KeyCode.LeftShift))
                 {
                     moveDirection.y = -speed;
                 }
 
                 PlayerRB.velocity = moveDirection;
 
             if(Input.GetButton("Vertical") || Input.GetButton("Horizontal") || Input.GetKey(KeyCode.LeftShift) || Input.GetButton("Jump"))
             {
                 PlayerRB.isKinematic = false;
                 if (frame > 5)
                 {
                     PlayerRB.isKinematic = true;
                     frame = 0;
                 }else
                 {
                     PlayerRB.isKinematic = false;
                     frame++;
                 }
             }else
             {
                 PlayerRB.isKinematic = true;
             }
             //end of move
         }
 
 
 
 
 
 
         //Near Planet
         if (onVehicle == false && nearPlanet == true)
         {
 
 
             //rotate
                 float mouseX = Input.GetAxis("Mouse X");
                 float mouseY = -Input.GetAxis("Mouse Y");
 
                 rotationY += mouseX * mouseSensitivity * Time.deltaTime;
                 rotationX += mouseY * mouseSensitivity * Time.deltaTime;
 
                 rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
 
                 Quaternion localRotation = Quaternion.Euler(0.0f, rotationY, 0.0f);
                 transform.rotation = localRotation;
                 //end of rotate
 
 
 
         }
 
 
 
 
 
 
 
     }
 }
 



This is the error I got NullReferenceException: Object reference not set to an instance of an object player_controller.Update () (at Assets/WarpSpace/Obj/ObjFile/Droid/Scripts/player_controller.cs:52)

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
1

Answer by Captain_Pineapple · Apr 14, 2018 at 09:24 AM

Depending on what you want to do:

If you just want remove the playerController Script on Objects that are not the currents Player property don't use destroy(this) but rather:

 getComponent<player_controller>().enabled = false;

If you really want to remove the GameObject as a whole then use Destroy(gameObject);

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 reimeguroinferno_unity · Apr 14, 2018 at 10:20 AM 0
Share

Thanks Alot! Although I did that, I still get the same error message. I removed that script and my islocalplayer just keeps alternating true and false after 3 frames of false when starting up. There are no scripts attatched to the 2 cameras I have.

avatar image Captain_Pineapple reimeguroinferno_unity · Apr 14, 2018 at 11:25 AM 0
Share

I'm not sure if i can help you with that problem since i'd probably need to see everything you do, especially since you use networking.

However a workaround for your case might be to just encase all unwanted code in the update function in an if-Statement:

 if(isLocalPlayer)
 { 
 // dostuff
 }

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

116 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

Related Questions

Strange GameObject null reference only in RPC 0 Answers

UNet: Server only non-player GameObject NullReferenceException on client 1 Answer

Why is my script variable NULL in a ClientRPC??? 1 Answer

Unity networking tutorial? 6 Answers

Networking-multplayer issue 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