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 Lynxxx · Sep 21, 2016 at 02:17 PM · multiplayernullreferenceexceptionreferenceinstantiationawake

Can't reference gameObject in NetworkBehaviour.Awake()

Hello Community

I am currently developing a little multiplayer game. But I stumbled over a problem I struggle with. As you see in the code below my player loads some other gameObjects in Awake(). Now I have the following behaviour:

-Hosting from Editor: In the editor everything works fine. On the standalone build tough it can't find the Script of the "Game"-Object and throws a NullReferenceException. -Hosting from Standalone: Now I get both in the editor and the standalone build the NullReferenceException mentioned above.

Here the code snippet:

public class PlayerController1 : NetworkBehaviour {

 public float speed;

 private GameObject game;
 private GameController gameScript;
 private GameObject ball;

 private Ray ray;
 private RaycastHit hit;
 private Vector3 playerStartPosition;

 public override void OnStartLocalPlayer() {
     gameObject.GetComponent<Renderer>().material.color = Color.blue;
 }

 void Awake() {
     ball = GameObject.FindGameObjectWithTag("Ball");    // Is null when hosted on standalone
     game = GameObject.FindGameObjectWithTag("Game");    // Is null when hosted on standalone
     gameScript = game.GetComponent<GameController>();    // NullReferenceError
 }

 void Start() {
     // set player start position
     playerStartPosition = transform.position;

     CmdClientReady();
 }

... }

From debugging in the editor I know that I can't even find the gameObjects when I host a game on the standalone build (which would explain the NullReferenceException). But as much as I know, when Awake() is called, all gameObjects should be instantiated right? Therefore I don't understand why it can't find the gameObjects/the script.

Thanks in advance for all responses. If you need more information, I will gladly provide it.

Sincerely, Simon

EDIT: Just to specify this a little closer: The problem is that the second GameObject "Game" is not found in Awake(). The "Ball" GameObject is found though! I also made a fix now that solves the problem: I use a coroutine that executes until all GameObjects are referenced. I also count how many times the coroutine is executed. Now the interesting thing is, that in the Editor the coroutine is executed only ONCE, whilst in the Standalone is executed TWICE. This would explain, why it is not working in the first place, as the second GameObject "Game" seems not to be found in the Standalone right at start. What I'm still curious about is, WHY it can't be found.

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 ScaniX · Sep 28, 2016 at 12:06 PM 0
Share

If I'm not mistaken, a basic rule is that it is safe to reference your own components in the Awake() and reference other game objects in the Start(). Don't know if that applies here, but would be worth a shot.

avatar image Lynxxx ScaniX · Sep 29, 2016 at 07:34 AM 0
Share

As much as I know all GameObjects are already initialized when Awake() gets called. I still tried, without success. The thing is, that the "Ball" GameObject is found whilst the "Game" GameObject is not.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by szimmermann · Sep 26, 2016 at 10:03 AM

Are you sure the game objects tagged with "Ball" and "Game" are active? FindGameObjectWithTag only returns active objects.

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 Lynxxx · Sep 28, 2016 at 12:04 PM 0
Share

Thanks for your reply! The objects are active in the scene and the tags are set correctly. The crazy thing is that the reference seems to work when I host the game with the Unity editor. If you have an idea what could possibly be wrong I'd be glad to hear it. Until then I will try to build a workaround with a coroutine that keeps repeating until the reference is set correctly.

avatar image
0

Answer by edo_m18 · Sep 28, 2016 at 01:48 PM

The "Ball" and "Game" are controlled by NetworkManager?
If so, Maybe you need to use ClientScene.FindLocalObject or NetworkServer.FindLocalObject methods.

A client working on the server, to use NetworkServer.FindLocalObject method.
Or a client working on the client, to use ClientScene.FindLocalObject method.

You consider to use these methods, You should use NetworkInstanceId as an argument for methods.

Like below.

 // for client
 GameObject target = ClientScene.FindLocalObject(netId);
 
 // for server
 GameObject target = NetworkServer.FindLocalObject(netId);

I was wondering these problem too.
I could see these object in the scene hierarchy, but I couldn't get them.

You need to seem other things is to get NetworkInstanceId from where.

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 Lynxxx · Sep 29, 2016 at 07:37 AM 0
Share

Hy edo_m18, The "Ball" and "Game" GameObjects are not controlled by the Network$$anonymous$$anager. Ins$$anonymous$$d they already exist in the Scene I load. Your tipp will come in hand though later in my project. Thanks.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object ? 1 Answer

Awake and start functions execute twice in multiplayer game 2 Answers

Camera Switching When Player Dies 1 Answer

Getting a reference to a group of ScriptableObjects implemented at runtime? 0 Answers

Trouble creating reference of image in script. 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