• 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
Question by J_Issa · Apr 28, 2014 at 09:39 PM · networkingbeginnernullreferenceexception

Not sure why I keep getting a Null Reference Exception? (Networking)

Hi there, I'm kind of new to the networking side of Unity and I'm using the basic Unity networking library.

I'm aiming to make my enemies fire a bullet because I am trying to make a Space invaders style game, but an issue I have is that I keep getting a NullReferenceException in one of my scripts.

My bullet is spawned but then when the bullet gets spawned the error appears and I can't test the game further. Here is the error I get:

NullReferenceException: Object reference not set to an instance of an object Game.Update () (at Assets/Scripts/Game.cs:57)

And here is my code snippets for the 2 scripts in question.

Game.cs Update function

public class Game : MonoBehaviour { public bool running;

 public List<GameObject> bullets;
 public List<GameObject> newBullets;
     
 public GameObject sphereEnemy, rotatorEnemy;
 private static int enemyCount;    
     
 private Rect bulletCountPos, resetGamePos, startGamePos, gameBoxPos, gameRunningPos;
     
 // Use this for initialization
 void Start () 
 {
     resetGamePos = new Rect(10, 70, 150, 20);        
     startGamePos = new Rect(10, 90, 150, 20);
         
     bullets = new List<GameObject>();
     newBullets = new List<GameObject>();
         
     bulletCountPos = new Rect(Screen.width - 200, 10, 200, 20);
     gameBoxPos = new Rect(Screen.width - 210, 10, 200, 130);    
     gameRunningPos = new Rect(Screen.width - 200, 30, 200, 20);    
 }
     
     // Update is called once per frame
     void Update () 
     {
         if (running)
         {
             if (Network.peerType == NetworkPeerType.Server)
             {
                 //Debug.Log("Updating bullet list");
                 
                 Bullet bulletScript;
                 EnemyBullet enemyBulletScript;
                 
                 newBullets = new List<GameObject>(bullets);
                 
                 bullets.Clear();
                 
                 // check all local bullet flight times
                 // Network.Destroy any over time
                 foreach(GameObject bullet in newBullets)
                 {
                     // check if bullet still exists (because the collision trigger messages can run during this update loop)
                     if (bullet == null) continue;
                     
                     bulletScript = (Bullet)bullet.GetComponent("Bullet");
                     enemyBulletScript = (EnemyBullet)bullet.GetComponent("EnemyBullet");
                     
                     if (bulletScript.currentFlightTime < bulletScript.maxFlightTime)
                     {
                         bullets.Add(bullet);
                     }
                     else
                     {
                         // tell all clients to destroy the bullet
                         Debug.Log("Ordering clients to remove " + bullet.name);
                         networkView.RPC("DestroyBullet", RPCMode.Others, bullet.name);
                         
                         // destroy local bullet on server
                         Destroy(bullet);
                     }
                 }                
                 
                 // clear the newBullets list
                 newBullets.Clear();
                 
                 //Debug.Log("Finished updating bullet list");
             }
         }        
     }


SmartEnemyController

// are we the server or a client?

if (Network.isServer)

{

     Debug.Log("Server is spawning EnemyBullet" + enemyBulletCount + " fired by " + gameObject.name);
                         
 // call FireBullet RPC in clients
 networkView.RPC("FireBullet", RPCMode.Others, "EnemyBullet"+enemyBulletCount);
                         
 // create bullet on local server                    
 GameObject bullet = (GameObject)Instantiate(enemyBulletPrefab, transform.position, enemyBulletPrefab.transform.rotation);
                         
 bullet.name = "EnemyBullet"+enemyBulletCount;
                         
 ((EnemyBullet)bullet.GetComponent("EnemyBullet")).setFiredBy(gameObject.name);
                         
 // add bullet to server list of bullets
 game.addBullet(bullet);
                         
 enemyBulletCount++;
 }
 else
 {
     networkView.RPC("Log", RPCMode.Server, "EnemyBullet fired"+enemyBulletCount);
                             
     // call server FireBullet RPC 
     networkView.RPC("ServerFireBullet", RPCMode.Server, gameObject.name);
 }

Thanks in advance for the help guys.

Comment

People who like this

0 Show 0
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Editor forgets elements of array after saving changes to code 0 Answers

Strange GameObject null reference only in RPC 0 Answers

Simple Easy Networking using Wireless Internet 1 Answer

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

Problem for moving an object with a button 0 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