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 rey lagarto · Aug 20, 2014 at 06:57 PM · error

NullReferenceException: Object reference not set to an instance of an object NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:46)

 using UnityEngine;
 using System.Collections;
 
 public class NetworkManager : MonoBehaviour {
     
     public Camera standbyCamera;
     SpawnSpot[] spawnSpots;
 
     // Use this for initialization
     void Start () {
         spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
         Connect ();    
     }
     
     void Connect() {
         PhotonNetwork.ConnectUsingSettings( "MultiPlayer v001" );    
     }
     
     void OnGUI() {
         GUILayout.Label( PhotonNetwork.connectionStateDetailed.ToString() );
     }
     
     void OnJoinedLobby(){
         PhotonNetwork.JoinRandomRoom();
     }
     
     void OnPhotonRandomJoinFailed(){
         PhotonNetwork.CreateRoom( null );
     }
     
     void OnJoinedRoom(){
         SpawnMyPlayer();
     }
     
     void SpawnMyPlayer(){
         if(spawnSpots == null){
             Debug.LogError ("QUE MIERDA!!!");
             return;
         }
         
         
         SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length)];
         GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player Controller", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
         standbyCamera.enabled = false;
         ((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
         ((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
         myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
     }
 }








error: NullReferenceException: Object reference not set to an instance of an object NetworkManager.SpawnMyPlayer () (at Assets/NetworkManager.cs:46) NetworkManager.OnJoinedRoom () (at Assets/NetworkManager.cs:32) UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions) NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1865) NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1708) ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[]) ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands() ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands() PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:83)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Aug 20, 2014 at 07:03 PM

Do yourself a favor and debug your code. It appears it can't find the MouseLook script, adjust your code to output to your console.

     SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length)];
     GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player Controller", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
     standbyCamera.enabled = false;
     
     MonoBehaviour fpsInput = myPlayerGO.GetComponent("FPSInputController") as MonoBehaviour;
     if (fpsInput != null)
     {
       fpsInput.enabled = true;
       Debug.Log("FPSInputController is enabled");
     }
     else
     {
       Debug.Log("Couldn't find a reference to FPSInputController on myPlayerGO");
     }
     //((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
     
     MonoBehaviour mouseLook = myPlayerGO.GetComponent("MouseLook") as MonoBehaviour;
     if(mouseLook != null)
     {
       mouseLook.enabled = true;
       Debug.Log("MouseLook is enabled");
     }
     else
     {
       Debug.Log("Couldn't find a reference to MouseLook on myPlayerGO");
     }
             
     //((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
     myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
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 rey lagarto · Aug 21, 2014 at 05:02 AM 0
Share

now unity gives me this error

Assets/Network$$anonymous$$anager.cs(73,1): error CS8025: Parsing error

why whats meens??

avatar image rey lagarto · Aug 21, 2014 at 06:25 AM 0
Share

thanks for your answer but now unity give me this error

Assets/Network$$anonymous$$anager.cs(66,92): error CS8025: Parsing error

whats meens??

avatar image KiraSensei · Aug 21, 2014 at 06:34 AM 0
Share

It means the line 66 has a parsing error. Since we don't know what looks likes this line, we can't help you.

avatar image
0

Answer by Andres-Fernandez · Aug 21, 2014 at 06:35 AM

It means you have some error in the NetworkManager script, before line 66. Check for missing parenthesis, brackets, semicolon, things like that.

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 MajorParts · Apr 26, 2015 at 12:11 AM 0
Share

This is a strange thing, as

(($$anonymous$$onoBehaviour)myPlayerGO.GetComponent("$$anonymous$$ouseLook")).enabled = true;

used to work. In fact it still does on one of my other projects. On another project, it does not. The code in the answer above with debug logs does fix it, but in a strange way! The log still reports that it can't find a reference to $$anonymous$$ouseLook, but $$anonymous$$ouseLook is being enabled. Using the code without the debug stuff....

$$anonymous$$onoBehaviour mouseLook = myPlayerGO.GetComponent("$$anonymous$$ouseLook") as $$anonymous$$onoBehaviour;

mouseLook.enabled = true;

makes it not work again! Anyone care to explain this? I'd like to know the logic here. Cheers

avatar image Andres-Fernandez · Apr 27, 2015 at 06:30 AM 0
Share

Well, it's been so long since this code was posted and we don't know what version of Unity you are using... But, one thing comes to my $$anonymous$$d: You are creating an instance of what I believe is a prefab of your player, right? Are you sure that the prefab doesn't come with the $$anonymous$$ouseLook component already enabled? Because that might be the case: new instance has the $$anonymous$$ouseLook component enabled, but GetComponent can't find the component itself.

Also, have yo tried getting the component as $$anonymous$$ouseLook ins$$anonymous$$d of $$anonymous$$onoBehaviour?

 $$anonymous$$ouseLook mouseLook = myPlayerGO.GetComponent<$$anonymous$$ouseLook>();
 if(mouseLook != null) {
    mouseLook.enabled = true;
 }
avatar image MajorParts · Apr 27, 2015 at 12:28 PM 0
Share

Thanks for the response! I've already fixed the issue with the code in the answer, even though as I said, it still reports not finding the $$anonymous$$ouseLook component. The network manager code originally came from a youtube tutorial and searching the problem gave many posts about the same code. I just don't understand why the same code works in my other project, but not this current one and countless other peoples projects. Also, why does the code in the answer above work and still report as though it doesn't? Both of my projects are using Unity 5

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with Input.touchCount == 1 1 Answer

UCE0001: ';' expected. Insert a semicolon at the end 1 Answer

Random Unity Errors I can't get rid of 1 Answer

Player move and camera problem 2 Answers

When i call some variables to an other second script i have a problem 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