• 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 brandonhotdo · Aug 11, 2014 at 04:39 PM · c#nullreferenceexceptionreturnvoid

NullReference

I have gotten a null reference exception on line 10

    using UnityEngine;
     using System.Collections;
     
     public class MainMenu : MonoBehaviour {
     public int ServerPort = 25565;
     public int MaxiPlayers = 27;
     
     void OnGUI(){
     if(GUI.Button(new Rect(Screen.width/2-310,Screen.height/2-175,183,28),"StartServer")){
     NetworkManager2.Instance.StartServer(MaxiPlayers,ServerPort);
     }
     }
   }

And on the script NetworkManager2

 using UnityEngine;
 using System.Collections;
 
 public class NetworkManager2 : MonoBehaviour {
     public static NetworkManager2 Instance;
 
     // Use this for initialization
     void Start () {
         Instance = this;
     }
     public void StartServer(int MaxPlayers, int Port){
         Debug.Log ("Good");
     }
 }
 

I don't know why I get the error! I heard that you can ignore it but then it does not do the void (It does not do what is in StartServer!).

Comment
Add comment · Show 2
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 robertbu · Aug 11, 2014 at 04:40 PM 0
Share

Is your Network$$anonymous$$anager2 script attached to a game object?

avatar image brandonhotdo · Aug 11, 2014 at 04:56 PM 0
Share

nope but it is now XD can't believe i forgot thx!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Aug 11, 2014 at 04:44 PM

you should check that NetworkManager2.Instance isn't null before you try to do anything with it.

instead of this:

 if(GUI.Button(new Rect(Screen.width/2-310,Screen.height/2-175,183,28),"StartServer"))
 {
     NetworkManager2.Instance.StartServer(MaxiPlayers,ServerPort);
 }

try this

 if (NetworkManager2.Instance != null)
 {
     if(GUI.Button(new Rect(Screen.width/2-310,Screen.height/2-175,183,28),"StartServer"))
     {
         NetworkManager2.Instance.StartServer(MaxiPlayers,ServerPort);
     }
 }

you might also try setting Instance in the Awake() function instead of Start()

Comment
Add comment · Show 1 · 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 brandonhotdo · Aug 11, 2014 at 04:55 PM 0
Share

Oh the problem was I forgot to put the network manager 2 in the acuttle scene soz for wasting your time XD

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object player.Update () (at Assets/player.cs:10) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Return Type Error? 1 Answer

NullReferenceException 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges