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 /
  • Help Room /
This question was closed Dec 11, 2015 at 09:46 PM by Exuro for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Exuro · Dec 11, 2015 at 07:42 PM · c#unity 5networking

Null Reference Exception when finding GameObject on Networked Game

I'm making a first person shooter using Unity Networking, which is something I've never used before, so I'm still trying to make sense of it. I have a GameObject in my Network Scene which is called DecalManager, and it is being used for the creation of things like bullet hole decals. There is a script attached to it which is a NetworkBehaviour, and contains data such as a list of all current bullet holes, to manage them and make sure there are no more than a certain amount on the server.

My issue is this; when I was using a Network Manager, it was working fine. The local players all store a reference to the servers decal manager, and when a gunshot hits a certain type of object, it calls a function in the decal manager to create a new one and the correct position. But then, I swapped it out for a Network Lobby Manager, and suddenly I'm getting a Null Reference Exception when it tries to find the reference. After some experimentation, I've determined that it is unable to find the DecalManager GameObject, even though I can definitely see that it is in the scene.

I have tried swapping the Network Lobby Manager back to a Network Manager to make sure I haven't changed anything else that would have broken it, and it started working again. So somehow, the simple act of using a Network Lobby Manager instead of a standard Network Manager has made it unable to find the object in the scene.

The relevant code in the player script, which is a Network Behaviour attached to all players:

 private DecalManager decalManager;
 
 void Start(){
             //This is the line that gives the null reference exception
         decalManager = GameObject.FindGameObjectWithTag("DecalManager").GetComponent<DecalManager> ();
 }

The decal manager script:

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DecalManager : NetworkBehaviour {
 
     [SerializeField] private int maxBulletHoles = 100; //Max amount of decals that can exist
     [SerializeField] private GameObject bhSprite; //Sprite image for bullet holes
 
     private List<GameObject> bulletHoles = new List<GameObject>();
 
     public void CreateBullethole(Vector3 position, Quaternion rotation)
     {
         if(bulletHoles.Count >= maxBulletHoles) //Has the maximum been reached?
         {
             NetworkServer.Destroy (bulletHoles[0]); //Destroy decal on server
             Destroy (bulletHoles[0]); //Destroy object locally
             bulletHoles.RemoveAt (0); //Remove from list
         }
 
         GameObject newHole = (GameObject)Instantiate (bhSprite, position, rotation); //Create new decal
 
         float randRot = Random.Range (0, 360);
         float randScale = Random.Range (0, 1);
 
         newHole.transform.RotateAround(newHole.transform.position, newHole.transform.forward, randRot);
 
         //TODO: MAKE DECALS APPEAR WITH SIZE VARIATIONS
 
         newHole.transform.parent = transform; //Set parent
         NetworkServer.Spawn(newHole); //Spawn on server
         bulletHoles.Add (newHole); //Add to array
     }
 }

Does anybody know why this could be happening?

Thanks for any assistance in advance.

Update: I have also found that this error is not occurring for clients connecting to the game, only for the player that is hosting the server.

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

  • Sort: 
avatar image
0
Best Answer

Answer by Exuro · Dec 11, 2015 at 09:45 PM

Okay, so I've figured it out, and it's exactly what I thought it was at the start. It was simply the fact that when the start function of the player was being called, the level hadn't totally loaded yet, so the object didn't exist.

I had tried to account for that before, but the error was still happening. However, I've just tried it again, and it's worked.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

55 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

Related Questions

Network Transport Layer API does not work with iOS to PC???? 0 Answers

uNet FPS Camera Error [C#] [Unity 5] 2 Answers

Start Server or Join Existing Server as client automatically 0 Answers

Check Ground Detection not working. 0 Answers

Commands, ClientRPCs and OnPlayerConnect doesnt work if i start the Unet Server with "Lan Server Only" 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